0

我正在尝试通过单击按钮重新绘制面板,但它没有发生。我尝试使用许多方法,如 setEnabled(true)、setVisible(true)、repaint(),但这些方法都不起作用。而且我还想在两个面板之间切换,比如,如果我点击“总投票”按钮,一个面板应该显示,当我点击“部门投票”时,另一个面板应该显示在同一个地方。

请帮忙。提前致谢。

我在这里提供代码:

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

   } 
    }
4

2 回答 2

3

第一个问题是您正在比较String值的对象引用而不是内容...

if(action == "TOTAL VOTE")

由于StringJava 管理 s 的方式,它们不太可能永远相等。

StringJava中的比较是使用String#equals

if ("TOTAL VOTE".equals(action)) 

您的第二个问题可能与setVisible可能不会使容器层次结构无效的事实有关,这会告诉布局框架需要更新容器。

你有两个解决方案,第一个是调用revalidate父容器,第二个更好的是使用 a CardLayout,它的设计正是你想要做的......

查看如何使用卡片布局以获取更多详细信息

运行代码后更新

你有一系列复杂的问题......

  • 你从来没有设置任何按钮的动作命令,所以当actionPerformed事件引发时,action实际上是null
  • RSummary扩展自JFrame,但您创建了第二个JFrame名为rframe. 这非常令人困惑,如果您没有处理正确的框架,可能会导致更多问题。

首先设置actionCommand按钮的属性...

JButton but1 = new JButton("TOTAL VOTE");
but1.setActionCommand("TOTAL VOTE");

然后从 中删除extends JFrameRSummary因为它什么都不做,只会增加混乱并且没有任何好处

将框架设置为不可见不会“关闭”它。而是使用JFrame#dispose

最后,在组件是拆分窗格的一部分时使其可见不会调整拆分分隔线的大小。完成更正后,您将需要手动移动分隔线。

您可以考虑在拆分窗格中放置一个空面板并使用 a CardLayout,将其他组件添加到其中,使用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();

    }

}
于 2013-10-26T08:07:08.403 回答
2

要切换面板,您可以执行以下操作:

rframe.remove (panelToRemove);
rframe.add(panelToShow);

这是您问题的答案还是您为什么要重新粉刷?

地理搜索

更新: 总结一下:MadProgrammer 是对的,你必须替换if(action == "TOTAL VOTE")if(action.equals("TOTAL VOTE"))(CLOSE 相同)

你必须添加一个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).

于 2013-10-26T08:11:54.770 回答