请教一下熟悉java Swing的高手,该怎么改这个小程序?

我的目的是点击"确定"按钮后,表格内容会在JPanel显示出来,请问怎么修改,多谢了publicclassTest{privateJFramejFrame=null;pr... 我的目的是 点击"确定"按钮后,表格内容会在JPanel 显示出来,请问怎么修改,多谢了
public class Test{
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
private JTable jtable = null;
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new java.awt.Dimension(640, 480));
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}

private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJScrollPane(),BorderLayout.CENTER);
}
return jContentPane;
}

private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.add(getJButton(), null);
}
return jPanel;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("确定");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {

String []columnNames ={"列名1","列名2","列名3"};
Object [][]data={
{"内容1","内容1","内容1"},
{"内容2","内容2","内容2"},
{"内容3","内容3","内容3"},
};
jtable = new JTable(data, columnNames);
}
});
}
return jButton;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane(getTable());
}
return jScrollPane;
}
private JTable getTable(){
if (jtable==null){
jtable=new JTable();
}
return jtable;
}
public static void main(String args[]) throws IOException {
Test qa = new Test();
JFrame frame = qa.getJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
展开
 我来答
wxy12580
2012-07-11
知道答主
回答量:19
采纳率:0%
帮助的人:5.6万
展开全部
在 jtable = new JTable(data, columnNames);后面加上两句
getJContentPane().add(new JScrollPane(jtable),BorderLayout.CENTER);//重新添加JScrollPane和JTable
getJFrame().validate();//再次布置其子组件,是修改生效
百度网友845f74e61
推荐于2016-10-23 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1622万
展开全部
帮你改了一下,表格有些特殊,相要操作里面的数据是通过TableModel来实现的。
------------------------------------------------------
import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import sun.awt.RepaintArea;
public class Test {
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
private JTable jtable = null;
private DefaultTableModel model = null;
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new java.awt.Dimension(640, 480));
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
}
return jContentPane;
}
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.add(getJButton(), null);
jPanel.add(getJScrollPane());
}
return jPanel;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("确定");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Object[][] data = { { "内容1", "内容1", "内容1" },
{ "内容2", "内容2", "内容2" }, { "内容3", "内容3", "内容3" }, };
// jtable = new JTable(data, columnNames);
for (int i = 0; i < data.length; i++) {
Object[] row = data[i];
model.addRow(row);
}
}
});
}
return jButton;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getTable());
}
return jScrollPane;
}
private JTable getTable() {
if (jtable == null) {
String[] columnNames = { "列名1", "列名2", "列名3" };
model = new DefaultTableModel(columnNames, 0);
jtable = new JTable(model);
}
return jtable;
}
public static void main(String args[]) throws IOException {
Test qa = new Test();
JFrame frame = qa.getJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
糖酥棍儿
2012-07-11 · TA获得超过692个赞
知道小有建树答主
回答量:393
采纳率:0%
帮助的人:388万
展开全部
import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.*;
public class Test{
private JFrame jFrame = null;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
private JTable jtable = null;
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setSize(new java.awt.Dimension(640, 480));
jFrame.setContentPane(getJContentPane());
}
return jFrame;
}

private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJScrollPane(),BorderLayout.CENTER);
}
return jContentPane;
}

private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.add(getJButton(), null);
}
return jPanel;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("确定");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jtable.setVisible(true);
}
});
}
return jButton;
}
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane(getTable());
}
return jScrollPane;
}
private JTable getTable(){
if (jtable==null){
String []columnNames ={"列名1","列名2","列名3"};
Object [][]data={
{"内容1","内容1","内容1"},
{"内容2","内容2","内容2"},
{"内容3","内容3","内容3"},
};
jtable = new JTable(data, columnNames);
jtable.setVisible(false);
}
return jtable;
}
public static void main(String args[]) throws IOException {
Test qa = new Test();
JFrame frame = qa.getJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

调整一下表格对象的初始化就行了
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式