java 用java创建一个超级简单的表格小程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1597592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a super simple table applet in java
提问by danwoods
I'm trying to create an applet which displays a simple table with no headers or other decoration. Could anyone be so kind as to show me the code for this? All the examples I've found haven't compiled or have included extra features which I don't need. A simple 2 x 2 table with empty cells and no headers is what I'm looking for. Thanks to all in advance...
我正在尝试创建一个小程序,它显示一个没有标题或其他装饰的简单表格。任何人都可以向我展示这个代码吗?我发现的所有示例都没有编译或包含我不需要的额外功能。我正在寻找一个带有空单元格且没有标题的简单 2 x 2 表格。提前谢谢大家...
Code for skaffman:
斯卡夫曼的代码:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class danTeamProject extends Applet implements ActionListener
{
char[][] charValues = new char[10][10];
danTable aTable;
boolean allowUserInput = false;
public void init()
{
Button BtnStart = new Button("Start");
BtnStart.addActionListener((ActionListener)this); //cast
this.add(BtnStart); //add action listener to button
aTable = new danTable();
aTable.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.black);
aTable.draw(g);
}
public void actionPerformed(ActionEvent arg0)
{
}
}
and
和
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class danTable extends JPanel
{
public danTable()
{
// Create with initial data
Object[][] cellData = {
{"row1-col1", "row1-col2"},
{"row2-col1", "row2-col2"}};
String[] columnNames = {"col1", "col2"};
JTable table = new JTable(cellData, columnNames);
}
}
回答by OscarRyz
I have modified the code you posted.
我已经修改了您发布的代码。
Read it as many times as needed until you understand what it does. See also the coding conventions ( the brackets and the naming of the variables )
根据需要多次阅读它,直到您了解它的作用。另请参阅编码约定(括号和变量命名)
I didn't change too much though, I just make it run.
不过我并没有改变太多,我只是让它运行。
Pay special attention to the difference between your code and this one ( they are not too much though ) Feel free to ask in case of doubts
特别注意你的代码和这个代码之间的区别(虽然它们不是太多)如有疑问,请随时提问
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DanTeamProject extends Applet {
char[][] charValues = new char[10][10];
DanTable aTable;
boolean allowUserInput = false;
public void init() {
Button btnStart = new Button("Start");
this.add(btnStart);
aTable = new DanTable();
this.add( aTable );
}
}
class DanTable extends JPanel {
public DanTable() {
Object[][] cellData = {
{"row1-col1", "row1-col2"},
{"row2-col1", "row2-col2"}};
String[] columnNames = {"col1", "col2"};
add( new JTable(cellData, columnNames) ) ;
}
}
Here's the HTML used to view it
这是用于查看它的 HTML
<applet code="DanTeamProject.class" width=100 height=140></applet>
Here's the output:
这是输出:
回答by camickr
Create a JTable and add the table to a JPanel (instead of a JScrollPane) and the header will not appear. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for working examples.
创建一个 JTable 并将表添加到 JPanel(而不是 JScrollPane),标题将不会出现。阅读 JTable API 并按照有关“如何使用表”的 Swing 教程的链接获取工作示例。


