请教一下熟悉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);
}
} 展开
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);
}
} 展开
3个回答
展开全部
帮你改了一下,表格有些特殊,相要操作里面的数据是通过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);
}
}
------------------------------------------------------
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);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
}
调整一下表格对象的初始化就行了
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);
}
}
调整一下表格对象的初始化就行了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询