功能大体是这样的,按左边的“查询1”到“查询5”,进入不同的查询条件界面,再按“子查询-1”到“子查询-5”,显示不同的查询结果,通过JTable来显示查询的结果。
但目前所遇到的问题时:在“子查询”条件下,需要先清除JTable的内容,再将新的查询结果显示,但使用JScrollpane将Jtable Remove后,Jtable不能调整列宽和滚动条的移动。
请各位高手看一下。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;public class MainPanel extends JFrame
{ /**
*
*/
private static final long serialVersionUID = 1L; private JPanel contentPanel; private JLabel conTitleLabel; private JTable table; private JScrollPane jspPanel; /**
* @param args
*/
public static void main(String[] args)
{
new MainPanel().setVisible(true);
} public MainPanel()
{
initUI();
} private void initUI()
{
this.setTitle("Panel test");
this.setBounds(getBounds(null, 800, 600));
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); initLeftPanel();
} private Rectangle getBounds(Rectangle r, int w, int h)
{
Rectangle rect = new Rectangle();
if (r == null)
{
r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
} rect.setBounds(r.x + (r.width - w) / 2, r.y + (r.height - h) / 2, w, h); return rect;
} private void initLeftPanel()
{
JPanel panel = new JPanel(new GridLayout(5, 1));
this.getContentPane().add(panel, BorderLayout.WEST); JButton but_1 = new JButton("查询_1");
JButton but_2 = new JButton("查询_2");
JButton but_3 = new JButton("查询_3");
JButton but_4 = new JButton("查询_4");
JButton but_5 = new JButton("查询_5"); panel.add(but_1);
panel.add(but_2);
panel.add(but_3);
panel.add(but_4);
panel.add(but_5); but_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(1);
}
});
but_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(2);
}
});
but_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(3);
}
});
but_4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(4);
}
});
but_5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(5);
}
}); } private void initCenterPanel(final int type)
{
if (contentPanel == null)
{
contentPanel = new JPanel();
}
else
{
contentPanel.removeAll();
}
contentPanel.setLayout(new BorderLayout());
this.getContentPane().add(contentPanel, BorderLayout.CENTER); JPanel topPanel = new JPanel(new BorderLayout());
contentPanel.add(topPanel, BorderLayout.NORTH);
topPanel.add(new JLabel("查询设置---===" + type, SwingConstants.CENTER), BorderLayout.NORTH); JPanel butPanel = new JPanel();
topPanel.add(butPanel, BorderLayout.CENTER); JButton but_1 = new JButton("子 查询_1");
JButton but_2 = new JButton("子 查询_2");
JButton but_3 = new JButton("子 查询_3"); butPanel.add(but_1);
butPanel.add(but_2);
butPanel.add(but_3); but_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 1);
}
});
but_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 2);
}
});
but_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 3);
}
}); if (type == 1 || type == 3)
{
JButton but_5 = new JButton("子 查询_5");
butPanel.add(but_5);
but_5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 5);
}
});
}
else if (type == 2 || type == 4)
{
JButton but_4 = new JButton("子 查询_4");
butPanel.add(but_4);
but_4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 4);
}
});
}
else if (type == 5)
{ } conTitleLabel = new JLabel("", SwingConstants.CENTER);
contentPanel.add(conTitleLabel, BorderLayout.SOUTH); // jspPanel = new JScrollPane();
// contentPanel.add(jspPanel, BorderLayout.CENTER); } /**
* @param controlType
* @param type
*/
private void processQueryButtonAction(int controlType, int type)
{
int row = 0;
int col = 0;
if (type == 1)
{
row = 6;
col = 10;
}
else if (type == 2)
{
row = 8;
col = 9;
}
else if (type == 3)
{
row = 10;
col = 5;
}
else if (type == 4)
{
row = 10;
col = 2;
}
else if (type == 5)
{
row = 5;
col = 10;
}
if (table != null)
{
table.removeAll();
table = null;
} if (table == null)
{
table = new JTable(row, col);
} if (random == null)
{
random = new Random();
}
int temp = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
temp += random.nextInt(200);
table.setValueAt(controlType + "," + type + "," + i + "," + j + "," + random.nextInt(200), i, j);
}
}
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); this.conTitleLabel.setText(controlType + "_,_" + type + "_,_" + temp); if (jspPanel != null)
{
jspPanel.removeAll();
}
jspPanel = new JScrollPane(table);
contentPanel.add(jspPanel, BorderLayout.CENTER);
contentPanel.revalidate();
} private Random random; private void processButtonAction(int type)
{
initCenterPanel(type); contentPanel.revalidate();
}
}
但目前所遇到的问题时:在“子查询”条件下,需要先清除JTable的内容,再将新的查询结果显示,但使用JScrollpane将Jtable Remove后,Jtable不能调整列宽和滚动条的移动。
请各位高手看一下。
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;public class MainPanel extends JFrame
{ /**
*
*/
private static final long serialVersionUID = 1L; private JPanel contentPanel; private JLabel conTitleLabel; private JTable table; private JScrollPane jspPanel; /**
* @param args
*/
public static void main(String[] args)
{
new MainPanel().setVisible(true);
} public MainPanel()
{
initUI();
} private void initUI()
{
this.setTitle("Panel test");
this.setBounds(getBounds(null, 800, 600));
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); initLeftPanel();
} private Rectangle getBounds(Rectangle r, int w, int h)
{
Rectangle rect = new Rectangle();
if (r == null)
{
r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
} rect.setBounds(r.x + (r.width - w) / 2, r.y + (r.height - h) / 2, w, h); return rect;
} private void initLeftPanel()
{
JPanel panel = new JPanel(new GridLayout(5, 1));
this.getContentPane().add(panel, BorderLayout.WEST); JButton but_1 = new JButton("查询_1");
JButton but_2 = new JButton("查询_2");
JButton but_3 = new JButton("查询_3");
JButton but_4 = new JButton("查询_4");
JButton but_5 = new JButton("查询_5"); panel.add(but_1);
panel.add(but_2);
panel.add(but_3);
panel.add(but_4);
panel.add(but_5); but_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(1);
}
});
but_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(2);
}
});
but_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(3);
}
});
but_4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(4);
}
});
but_5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processButtonAction(5);
}
}); } private void initCenterPanel(final int type)
{
if (contentPanel == null)
{
contentPanel = new JPanel();
}
else
{
contentPanel.removeAll();
}
contentPanel.setLayout(new BorderLayout());
this.getContentPane().add(contentPanel, BorderLayout.CENTER); JPanel topPanel = new JPanel(new BorderLayout());
contentPanel.add(topPanel, BorderLayout.NORTH);
topPanel.add(new JLabel("查询设置---===" + type, SwingConstants.CENTER), BorderLayout.NORTH); JPanel butPanel = new JPanel();
topPanel.add(butPanel, BorderLayout.CENTER); JButton but_1 = new JButton("子 查询_1");
JButton but_2 = new JButton("子 查询_2");
JButton but_3 = new JButton("子 查询_3"); butPanel.add(but_1);
butPanel.add(but_2);
butPanel.add(but_3); but_1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 1);
}
});
but_2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 2);
}
});
but_3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 3);
}
}); if (type == 1 || type == 3)
{
JButton but_5 = new JButton("子 查询_5");
butPanel.add(but_5);
but_5.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 5);
}
});
}
else if (type == 2 || type == 4)
{
JButton but_4 = new JButton("子 查询_4");
butPanel.add(but_4);
but_4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
processQueryButtonAction(type, 4);
}
});
}
else if (type == 5)
{ } conTitleLabel = new JLabel("", SwingConstants.CENTER);
contentPanel.add(conTitleLabel, BorderLayout.SOUTH); // jspPanel = new JScrollPane();
// contentPanel.add(jspPanel, BorderLayout.CENTER); } /**
* @param controlType
* @param type
*/
private void processQueryButtonAction(int controlType, int type)
{
int row = 0;
int col = 0;
if (type == 1)
{
row = 6;
col = 10;
}
else if (type == 2)
{
row = 8;
col = 9;
}
else if (type == 3)
{
row = 10;
col = 5;
}
else if (type == 4)
{
row = 10;
col = 2;
}
else if (type == 5)
{
row = 5;
col = 10;
}
if (table != null)
{
table.removeAll();
table = null;
} if (table == null)
{
table = new JTable(row, col);
} if (random == null)
{
random = new Random();
}
int temp = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
temp += random.nextInt(200);
table.setValueAt(controlType + "," + type + "," + i + "," + j + "," + random.nextInt(200), i, j);
}
}
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); this.conTitleLabel.setText(controlType + "_,_" + type + "_,_" + temp); if (jspPanel != null)
{
jspPanel.removeAll();
}
jspPanel = new JScrollPane(table);
contentPanel.add(jspPanel, BorderLayout.CENTER);
contentPanel.revalidate();
} private Random random; private void processButtonAction(int type)
{
initCenterPanel(type); contentPanel.revalidate();
}
}
解决方案 »
- Java swing疑问
- 出现java.lang.String cannot be cast to java.util.Vector错误。。。求解决~~~
- 求一个java题目
- 一个完整地spring这么做对吗?
- JVM线程同步机制
- 帮忙啊,能不能举一个String类中的:compareTo(Object o)和compareTo(String anotherString) 的例子,
- 【求助:JDBC是否可以直接连接 .db 数据文件?】
- 急求JBuilder4的序列号和注册码
- 学java用什么软件写?
- weblogic反应这么迟钝!!!
- java 窗口居中问题
- ant 有关为<taskdef> 设置classpath的问题
而是JScrollPane jspPanel = new JScrollPane(table);import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;public class MainPanel extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPanel;
private JLabel conTitleLabel;
private JTable table;
private JScrollPane jspPanel; public static void main(String[] args) {
new MainPanel().setVisible(true);
} public MainPanel() {
initUI();
} private void initUI() {
this.setTitle("Panel test");
this.setBounds(getBounds(null, 800, 600));
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
initLeftPanel();
} private Rectangle getBounds(Rectangle r, int w, int h) {
Rectangle rect = new Rectangle();
if (r == null) {
r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}
rect.setBounds(r.x + (r.width - w) / 2, r.y + (r.height - h) / 2, w, h);
return rect;
} private void initLeftPanel() {
JPanel panel = new JPanel(new GridLayout(5, 1));
this.getContentPane().add(panel, BorderLayout.WEST); JButton but_1 = new JButton("query_1");
JButton but_2 = new JButton("query_2");
JButton but_3 = new JButton("query_3");
JButton but_4 = new JButton("query_4");
JButton but_5 = new JButton("query_5");
panel.add(but_1);
panel.add(but_2);
panel.add(but_3);
panel.add(but_4);
panel.add(but_5);
but_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processButtonAction(1);
}
});
but_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processButtonAction(2);
}
});
but_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processButtonAction(3);
}
});
but_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processButtonAction(4);
}
});
but_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processButtonAction(5);
}
});
} private void initCenterPanel(final int type) {
if (contentPanel == null) {
contentPanel = new JPanel();
} else {
contentPanel.removeAll();
}
contentPanel.setLayout(new BorderLayout());
this.getContentPane().add(contentPanel, BorderLayout.CENTER);
JPanel topPanel = new JPanel(new BorderLayout());
contentPanel.add(topPanel, BorderLayout.NORTH);
topPanel.add(new JLabel("query setting===" + type,
SwingConstants.CENTER), BorderLayout.NORTH);
JPanel butPanel = new JPanel();
topPanel.add(butPanel, BorderLayout.CENTER);
JButton but_1 = new JButton("sub query_1");
JButton but_2 = new JButton("sub query_2");
JButton but_3 = new JButton("sub query_3");
butPanel.add(but_1);
butPanel.add(but_2);
butPanel.add(but_3);
but_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processQueryButtonAction(type, 1);
}
});
but_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processQueryButtonAction(type, 2);
}
});
but_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processQueryButtonAction(type, 3);
}
});
if (type == 1 || type == 3) {
JButton but_5 = new JButton("sub query_5");
butPanel.add(but_5);
but_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processQueryButtonAction(type, 5);
}
});
} else if (type == 2 || type == 4) {
JButton but_4 = new JButton("sub query_4");
butPanel.add(but_4);
but_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
processQueryButtonAction(type, 4);
}
});
} else if (type == 5) {
}
conTitleLabel = new JLabel("", SwingConstants.CENTER);
contentPanel.add(conTitleLabel, BorderLayout.SOUTH);
} private void processQueryButtonAction(int controlType, int type) {
int row = 0;
int col = 0;
if (type == 1) {
row = 6;
col = 10;
} else if (type == 2) {
row = 8;
col = 9;
} else if (type == 3) {
row = 10;
col = 5;
} else if (type == 4) {
row = 10;
col = 2;
} else if (type == 5) {
row = 5;
col = 10;
}
if (table != null) {
table.removeAll();
table = null;
}
if (table == null) {
table = new JTable(row, col);
}
if (random == null) {
random = new Random();
}
int temp = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
temp += random.nextInt(200);
table.setValueAt(controlType + "," + type + "," + i + "," + j
+ "," + random.nextInt(200), i, j);
}
}
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.conTitleLabel.setText(controlType + "_,_" + type + "_,_" + temp);
if (jspPanel != null) {
jspPanel.removeAll();
}
//jspPanel = new JScrollPane(table);
JScrollPane jspPanel = new JScrollPane(table);
contentPanel.add(jspPanel, BorderLayout.CENTER);
contentPanel.revalidate();
} private Random random; private void processButtonAction(int type) {
initCenterPanel(type);
contentPanel.revalidate();
}
}
{
if (contentPanel == null)
{
contentPanel = new JPanel();
}
else
{
contentPanel.removeAll();
contentPanel.repaint(); //没有重绘。。加这一句就ok了··
}
contentPanel.setLayout(new BorderLayout());
this.getContentPane().add(contentPanel, BorderLayout.CENTER);
问题1:如果对JScrollpane 进行removeAll(),则会造成表格不能操作,不能设置列宽,滚动等。
问题2:如果不removeAll()操作,则表格可以操作,但在操作时,表格数据会恢复到 第一次 按 “子查询”的数据上,并且表格无法刷新。另外:使用removeAll()后,表格有数据时,如果按最大化,表格数据没有绘制。
请各位看一下。
谢谢大家的帮助。