用java创建一个超级简单的表格小程序

发布于 2024-08-08 04:52:09 字数 1162 浏览 0 评论 0 原文

我正在尝试创建一个小程序,它显示一个没有标题或其他装饰的简单表格。有人可以好心地向我展示这个代码吗?我发现的所有示例都没有编译或包含我不需要的额外功能。我正在寻找一个带有空单元格且没有标题的简单 2 x 2 表格。提前感谢所有人...

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)
{

}

}

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);

}
}

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...

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);

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-08-15 04:52:09

我已经修改了您发布的代码。

根据需要多次阅读,直到您理解它的作用。另请参阅编码约定(括号和变量的命名),

但我没有改变太多,我只是让它运行。

请特别注意您的代码和此代码之间的差异(虽然它们并没有太大),如有疑问,请随时询问

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) ) ;
    }
}

这是用于查看它的 HTML

<applet code="DanTeamProject.class" width=100 height=140></applet>

这是输出:

替代文字

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

<applet code="DanTeamProject.class" width=100 height=140></applet>

Here's the output:

alt text

兲鉂ぱ嘚淚 2024-08-15 04:52:09

创建一个 JTable 并将该表添加到 JPanel(而不是 JScrollPane),并且标题将不会出现。阅读 JTable API 并点击有关“如何使用表”的 Swing 教程的链接来获取工作示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文