Java 如何通过单击按钮重新绘制jpanel,并在两个面板之间切换
我试图通过点击按钮来重新绘制面板,但它没有发生。我尝试使用许多方法,如setEnabled(true)、setVisible(true)、repaint(),但这些方法都不起作用。我还想在两个面板之间切换,比如,如果我点击“总投票”按钮,一个面板应该显示,当我点击“部门投票”时,另一个面板应该显示在相同的位置 请帮忙。提前谢谢 我在这里提供代码:Java 如何通过单击按钮重新绘制jpanel,并在两个面板之间切换,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,我试图通过点击按钮来重新绘制面板,但它没有发生。我尝试使用许多方法,如setEnabled(true)、setVisible(true)、repaint(),但这些方法都不起作用。我还想在两个面板之间切换,比如,如果我点击“总投票”按钮,一个面板应该显示,当我点击“部门投票”时,另一个面板应该显示在相同的位置 请帮忙。提前谢谢 我在这里提供代码: public class RSummary extends JFrame implements ActionListener { JFrame
public class RSummary extends JFrame implements ActionListener
{
JFrame rframe = new JFrame();
JPanel panel1, panel2, panel3, panel4, panel5;
JTable table;
JScrollPane scroll;
JSplitPane splitPane;
JButton butx;
public RSummary()
{
rframe.setSize(550,300);
rframe.setLocationRelativeTo(null);
setSize(550,300);
rframe.setTitle("Summary Report");
/* Total Vote & Department wise vote buttons */
panel1= new JPanel();
JButton but1 = new JButton("TOTAL VOTE");
JButton but2 = new JButton("DEPTARTMENT WISE VOTE");
but1.addActionListener(this);
panel1.add(but1);
panel1.add(but2);
/* Report contents according to button */
panel2 = new JPanel();
String[] columnNames = {"Name", "Department","Phno","LUID","Select"};
DefaultTableModel model1 = new DefaultTableModel();
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
panel2.add(scroll);
panel2.setVisible(false);
/* close button */
panel3 = new JPanel();
JButton but4= new JButton("CLOSE");
but4.addActionListener(this);
panel3.add(but4);
/* Page heading */
panel4 = new JPanel();
JLabel lab =new JLabel("VOTE SUMMARY REPORT");
panel4.add(lab);
/* dummy panel */
panel5 = new JPanel();
JButton butx = new JButton("Hello butx");
panel5.add(butx);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
rframe.add(splitPane);
rframe.add(panel3,BorderLayout.SOUTH);
rframe.add(panel4,BorderLayout.NORTH);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String action=ae.getActionCommand();
if(action == "CLOSE")
{
rframe.setVisible(false);
}
if(action == "TOTAL VOTE")
{
panel2.setVisible(true); //this code is not working, i want the solution here.
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
new RSummary();
}
}
第一个问题是,您正在比较
字符串值的对象引用,而不是其中的内容
if(action == "TOTAL VOTE")
由于Java管理String
s的方式,它们不太可能相等
Java中的String
比较是使用String#equals
if ("TOTAL VOTE".equals(action))
第二个问题可能与以下事实有关:setVisible
可能不会使容器层次结构无效,这将告诉布局框架需要更新容器
您有两种解决方案,第一种是在父容器上调用revalidate
,第二种更好的解决方案是使用CardLayout
,它的设计正是为了满足您的需要
请查看以了解更多详细信息
运行代码后更新
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RSummary extends JFrame implements ActionListener {
JFrame rframe = new JFrame();
JPanel panel1, panel2, panel3, panel4, panel5;
JTable table;
JScrollPane scroll;
JSplitPane splitPane;
JButton butx;
public RSummary() {
rframe.setSize(550, 300);
rframe.setLocationRelativeTo(null);
setSize(550, 300);
rframe.setTitle("Summary Report");
/* Total Vote & Department wise vote buttons */
panel1 = new JPanel();
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
JButton but2 = new JButton("DEPTARTMENT WISE VOTE");
but1.addActionListener(this);
panel1.add(but1);
panel1.add(but2);
/* Report contents according to button */
panel2 = new JPanel();
String[] columnNames = {"Name", "Department", "Phno", "LUID", "Select"};
DefaultTableModel model1 = new DefaultTableModel();
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
panel2.add(scroll);
panel2.setVisible(false);
/* close button */
panel3 = new JPanel();
JButton but4 = new JButton("CLOSE");
but4.addActionListener(this);
panel3.add(but4);
/* Page heading */
panel4 = new JPanel();
JLabel lab = new JLabel("VOTE SUMMARY REPORT");
panel4.add(lab);
/* dummy panel */
panel5 = new JPanel();
JButton butx = new JButton("Hello butx");
panel5.add(butx);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
rframe.add(splitPane);
rframe.add(panel3, BorderLayout.SOUTH);
rframe.add(panel4, BorderLayout.NORTH);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if ("CLOSE".equals(action)) {
rframe.dispose();
}
if ("TOTAL VOTE".equals(action)) {
panel2.setVisible(true); //this code is not working, i want the solution here.
panel2.getParent().revalidate();
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException {
new RSummary();
}
}
你有一系列的复合问题
- 您从未设置任何按钮的操作命令,因此当引发
actionPerformed
事件时,action
实际上是null
RSummary
从JFrame
扩展而来,但是您创建了第二个JFrame
名为rframe
。这非常令人困惑,如果处理的框架不正确,可能会导致更多问题
首先设置按钮的actionCommand
属性
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
然后从RSummary
中删除extends JFrame
,因为它只会增加混乱,没有任何好处
将框架设置为不可见不会“关闭”它。改用JFrame#dispose
最后,使组件成为拆分窗格的一部分时可见不会调整拆分分隔符的大小。完成校正后,需要手动移动分隔器
您可以考虑将一个空面板放置到分割窗格中,并使用<代码>卡片布局< /代码>,将其他组件添加到它中,使用<代码>卡片布局< /COD>
将其转换为输入和输出。
用修改过的代码更新
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RSummary extends JFrame implements ActionListener {
JFrame rframe = new JFrame();
JPanel panel1, panel2, panel3, panel4, panel5;
JTable table;
JScrollPane scroll;
JSplitPane splitPane;
JButton butx;
public RSummary() {
rframe.setSize(550, 300);
rframe.setLocationRelativeTo(null);
setSize(550, 300);
rframe.setTitle("Summary Report");
/* Total Vote & Department wise vote buttons */
panel1 = new JPanel();
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
JButton but2 = new JButton("DEPTARTMENT WISE VOTE");
but1.addActionListener(this);
panel1.add(but1);
panel1.add(but2);
/* Report contents according to button */
panel2 = new JPanel();
String[] columnNames = {"Name", "Department", "Phno", "LUID", "Select"};
DefaultTableModel model1 = new DefaultTableModel();
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
panel2.add(scroll);
panel2.setVisible(false);
/* close button */
panel3 = new JPanel();
JButton but4 = new JButton("CLOSE");
but4.addActionListener(this);
panel3.add(but4);
/* Page heading */
panel4 = new JPanel();
JLabel lab = new JLabel("VOTE SUMMARY REPORT");
panel4.add(lab);
/* dummy panel */
panel5 = new JPanel();
JButton butx = new JButton("Hello butx");
panel5.add(butx);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
rframe.add(splitPane);
rframe.add(panel3, BorderLayout.SOUTH);
rframe.add(panel4, BorderLayout.NORTH);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if ("CLOSE".equals(action)) {
rframe.dispose();
}
if ("TOTAL VOTE".equals(action)) {
panel2.setVisible(true); //this code is not working, i want the solution here.
panel2.getParent().revalidate();
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException {
new RSummary();
}
}
第一个问题是,您正在比较字符串值的对象引用,而不是其中的内容
if(action == "TOTAL VOTE")
由于Java管理String
s的方式,它们不太可能相等
Java中的String
比较是使用String#equals
if ("TOTAL VOTE".equals(action))
第二个问题可能与以下事实有关:setVisible
可能不会使容器层次结构无效,这将告诉布局框架需要更新容器
您有两种解决方案,第一种是在父容器上调用revalidate
,第二种更好的解决方案是使用CardLayout
,它的设计正是为了满足您的需要
请查看以了解更多详细信息
运行代码后更新
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RSummary extends JFrame implements ActionListener {
JFrame rframe = new JFrame();
JPanel panel1, panel2, panel3, panel4, panel5;
JTable table;
JScrollPane scroll;
JSplitPane splitPane;
JButton butx;
public RSummary() {
rframe.setSize(550, 300);
rframe.setLocationRelativeTo(null);
setSize(550, 300);
rframe.setTitle("Summary Report");
/* Total Vote & Department wise vote buttons */
panel1 = new JPanel();
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
JButton but2 = new JButton("DEPTARTMENT WISE VOTE");
but1.addActionListener(this);
panel1.add(but1);
panel1.add(but2);
/* Report contents according to button */
panel2 = new JPanel();
String[] columnNames = {"Name", "Department", "Phno", "LUID", "Select"};
DefaultTableModel model1 = new DefaultTableModel();
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
panel2.add(scroll);
panel2.setVisible(false);
/* close button */
panel3 = new JPanel();
JButton but4 = new JButton("CLOSE");
but4.addActionListener(this);
panel3.add(but4);
/* Page heading */
panel4 = new JPanel();
JLabel lab = new JLabel("VOTE SUMMARY REPORT");
panel4.add(lab);
/* dummy panel */
panel5 = new JPanel();
JButton butx = new JButton("Hello butx");
panel5.add(butx);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
rframe.add(splitPane);
rframe.add(panel3, BorderLayout.SOUTH);
rframe.add(panel4, BorderLayout.NORTH);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if ("CLOSE".equals(action)) {
rframe.dispose();
}
if ("TOTAL VOTE".equals(action)) {
panel2.setVisible(true); //this code is not working, i want the solution here.
panel2.getParent().revalidate();
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException {
new RSummary();
}
}
你有一系列的复合问题
- 您从未设置任何按钮的操作命令,因此当引发
actionPerformed
事件时,action
实际上是null
RSummary
从JFrame
扩展而来,但是您创建了第二个JFrame
名为rframe
。这非常令人困惑,如果处理的框架不正确,可能会导致更多问题
首先设置按钮的actionCommand
属性
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
然后从RSummary
中删除extends JFrame
,因为它只会增加混乱,没有任何好处
将框架设置为不可见不会“关闭”它。改用JFrame#dispose
最后,使组件成为拆分窗格的一部分时可见不会调整拆分分隔符的大小。完成校正后,需要手动移动分隔器
您可以考虑将一个空面板放置到分割窗格中,并使用<代码>卡片布局< /代码>,将其他组件添加到它中,使用<代码>卡片布局< /COD>
将其转换为输入和输出。
用修改过的代码更新
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class RSummary extends JFrame implements ActionListener {
JFrame rframe = new JFrame();
JPanel panel1, panel2, panel3, panel4, panel5;
JTable table;
JScrollPane scroll;
JSplitPane splitPane;
JButton butx;
public RSummary() {
rframe.setSize(550, 300);
rframe.setLocationRelativeTo(null);
setSize(550, 300);
rframe.setTitle("Summary Report");
/* Total Vote & Department wise vote buttons */
panel1 = new JPanel();
JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");
JButton but2 = new JButton("DEPTARTMENT WISE VOTE");
but1.addActionListener(this);
panel1.add(but1);
panel1.add(but2);
/* Report contents according to button */
panel2 = new JPanel();
String[] columnNames = {"Name", "Department", "Phno", "LUID", "Select"};
DefaultTableModel model1 = new DefaultTableModel();
model1.setColumnIdentifiers(columnNames);
table = new JTable();
table.setModel(model1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
panel2.add(scroll);
panel2.setVisible(false);
/* close button */
panel3 = new JPanel();
JButton but4 = new JButton("CLOSE");
but4.addActionListener(this);
panel3.add(but4);
/* Page heading */
panel4 = new JPanel();
JLabel lab = new JLabel("VOTE SUMMARY REPORT");
panel4.add(lab);
/* dummy panel */
panel5 = new JPanel();
JButton butx = new JButton("Hello butx");
panel5.add(butx);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1, panel2);
rframe.add(splitPane);
rframe.add(panel3, BorderLayout.SOUTH);
rframe.add(panel4, BorderLayout.NORTH);
rframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rframe.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
String action = ae.getActionCommand();
if ("CLOSE".equals(action)) {
rframe.dispose();
}
if ("TOTAL VOTE".equals(action)) {
panel2.setVisible(true); //this code is not working, i want the solution here.
panel2.getParent().revalidate();
}
}
public static void main(String args[]) throws ClassNotFoundException, SQLException {
new RSummary();
}
}
要切换面板,可以执行以下操作:
rframe.remove (panelToRemove);
rframe.add(panelToShow);
这就是你问题的答案,还是你为什么要重画
地球科学家
更新:
总结如下:
如果程序员是对的,您必须将if(action==“TOTAL VOTE”)
替换为if(action.equals(“TOTAL VOTE”)
(结束时相同)
您必须添加一个actionCommand:
JButton but1 = new JButton("TOTAL VOTE");
but1.addActionListener(this);
but1.setActionCommand("TOTAL VOTE");
要更改面板,请执行以下操作:
if (action.equals("TOTAL VOTE")) {
try{rframe.remove(panel1;)}catch(Exception e){}//clear window, maybe there is a method
try{rframe.remove(panel2;)}catch(Exception e){}
try{rframe.remove(panel3;)}catch(Exception e){}
rframe.add(panel2);//the panel you want to show
}
当然,您必须获得对面板的引用,以actionPerformed(ActionEvent ae)
切换面板,您可以执行以下操作:
rframe.remove (panelToRemove);
rframe.add(panelToShow);
这就是你问题的答案,还是你为什么要重画
地球科学家
更新:
总结如下:
如果程序员是对的,您必须将if(action==“TOTAL VOTE”)
替换为if(action.equals(“TOTAL VOTE”)
(结束时相同)
您必须添加一个actionCommand:
JButton but1 = new JButton("TOTAL VOTE");
but1.addActionListener(this);
but1.setActionCommand("TOTAL VOTE");
要更改面板,请执行以下操作:
if (action.equals("TOTAL VOTE")) {
try{rframe.remove(panel1;)}catch(Exception e){}//clear window, maybe there is a method
try{rframe.remove(panel2;)}catch(Exception e){}
try{rframe.remove(panel3;)}catch(Exception e){}
rframe.add(panel2);//the panel you want to show
}
当然,您必须获得对actionPerformed(ActionEvent ae)
面板的引用,这没有效果,我也尝试过。如果是这种情况,那么我的其他按钮不应该工作,但其他按钮工作正常。我也尝试了“if(“TOTAL VOTE.equals(action))”语句,但它不工作working@user2909960检查