【问题标题】:make several JPanels and their JTables fit the JFrame使多个 JPanel 及其 JTable 适合 JFrame
【发布时间】:2014-01-05 21:41:04
【问题描述】:

我知道之前发布了很多类似的问题,但没有一个对我有帮助。 我创建了一个JFrame,里面有几个JPanel-s,这里包括一些里面有几个JTable-s。但我不能让它适合JFrame。我试过pack();这不起作用,我尝试添加JScrollPane,这也不能解决问题。我是使用 GUI 的新手,所以布局选项有点难以弄清楚,也许这可能是问题所在?

代码如下:

public class Frame extends JFrame implements ActionListener{
    private JPanel mainScreen, menu1, menu2, menu3;
    private JButton defTeamsButton, userTeamsButton, menu1Button1, menu2Button1, menu1backButton, menu2backButton,menu3backButton;
    private JTable table1, table2, table3;
    private JScrollPane scrollPane, scrollPane2, scrollPane3;
    private JScrollBar bar;
    private JTextField jtfDay;
    private  JLabel jlblDay;

    public Frame(){
        setLayout(new GridLayout(2,0));
        mainScreen = new JPanel();

        defTeamsButton = new JButton("Default/ preset teams");
        defTeamsButton.setLocation(0, 20);
        defTeamsButton.setSize(250, 30);

        userTeamsButton = new JButton("Add my own teams");
        userTeamsButton.setLocation(0, 60);
        userTeamsButton.setSize(250, 30);

        menu1 = new JPanel();

        menu1Button1 = new JButton("Display fixture table");
        menu1Button1.setLocation(0, 20);
        menu1Button1.setSize(250, 30);
        menu1backButton = new JButton("Back");

        menu2Button1 = new JButton("Display standings table");
        menu2Button1.setLocation(0, 40);
        menu2Button1.setSize(250, 30);


        menu2 = new JPanel();
        table1 = new JTable(new tables());
        scrollPane = new JScrollPane(table1);
        scrollPane.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
               "Sunday 1",
               TitledBorder.CENTER,
               TitledBorder.TOP));
        table2 = new JTable(new tables2());
        table2.setSize(300, 200);

        scrollPane2 = new JScrollPane(table2);
        scrollPane2.setBorder (BorderFactory.createTitledBorder (BorderFactory.createEtchedBorder (),
                 "Wednesday 1",
                 TitledBorder.CENTER,
                 TitledBorder.TOP));
        menu2backButton = new JButton("Back");

        menu3 = new JPanel();
        table3 = new JTable(new tables3());
        scrollPane3 = new JScrollPane(table3);
        menu3backButton = new JButton("Back");

        mainScreen.add(defTeamsButton);
        mainScreen.add(userTeamsButton);
        menu1.add(menu1Button1);
        menu1.add(menu2Button1);
        menu1.add(menu1backButton);
        menu2.add(menu2backButton);
        menu2.add(scrollPane);
        menu2.add(scrollPane2);
        menu3.add(scrollPane3);
        menu3.add(menu3backButton);
        add(mainScreen);

        add(mainScreen);

        //register listener with buttons
        defTeamsButton.addActionListener(this); 
        userTeamsButton.addActionListener(this); 
        menu1Button1.addActionListener(this);
        menu2Button1.addActionListener(this);
        menu1backButton.addActionListener(this);
        menu2backButton.addActionListener(this);
        menu3backButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e){
       if(e.getSource() == defTeamsButton){
                //define action for defTeamsButton
            getContentPane().removeAll();
            getContentPane().add(menu1);//Adding to content pane, not to Frame
            repaint();
            printAll(getGraphics());
            }   

            else if(e.getSource() == userTeamsButton)
            {

                //ask user to input teams, to do..
                String Team1= "";
                String Team2= "";
                String Team3= "";
                String Team4= "";
                Team1 = JOptionPane.showInputDialog(
                        null, "Please enter your First Team: ");
                Team2 = JOptionPane.showInputDialog(
                        null, "Please enter your First Team: ");
                Team3 = JOptionPane.showInputDialog(
                        null, "Please enter your First Team: ");
                Team4 = JOptionPane.showInputDialog(
                        null, "Please enter your First Team: ");
                JOptionPane.showMessageDialog (null,"  your teams are" + Team1 + Team2 
                        + Team3 + Team4);


            }
            else if(e.getSource() == menu1Button1){
                getContentPane().removeAll();
                getContentPane().add(menu2);//Adding to content pane, not to Frame
                repaint();
                printAll(getGraphics());
                }   
        else if(e.getSource() == menu2Button1)
        {
            getContentPane().removeAll();
            getContentPane().add(menu3);//Adding to content pane, not to Frame
            repaint();
            printAll(getGraphics());
        }
       else if(e.getSource() == menu1backButton)
       {
           getContentPane().removeAll();
           getContentPane().add(mainScreen);//Adding to content pane, not to Frame

           repaint();
           printAll(getGraphics());

       }
       else if(e.getSource() == menu2backButton)
       {
           getContentPane().removeAll();
           getContentPane().add(menu1);//Adding to content pane, not to Frame

           repaint();
           printAll(getGraphics());
       }
    }

    public  class tables extends AbstractTableModel {
        /**
         * 
         */

        private String[] columnNames = {"Home",
                                        "Away",};

        private String[][] data = {
        {"Kathy", "Smith"},
        {"John", "Doe"},
        {"Sue", "Black"},
        {"Jane", "White"},
        {"Joe", "Brown"}
        };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }


    }
    public  class tables2 extends AbstractTableModel {
        /**
         * 
         */

        private String[] columnNames = {"Home",
                                        "Away",
                                        };

        private String[][] data = {
        {"Team A", "Team B"},
        {"Team C", "Team D"},
        {"Team E", "Team F"},
        {"Team G", "Team H"},
        {"Team I", "Team H"}
        };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }


      }

    public  class tables3 extends AbstractTableModel {
        /**
         * 
         */

        private String[] columnNames = {"Teams",
                                        "Goals",
                                        "GoalDifference", 
                                        "Score"
                                        };

        private Object[][] data = {
        {"Kathy", new Integer(5), new Integer(5), new Integer(5)},
        {"John", new Integer(5), new Integer(4), new Integer(5)},
        {"Sue", new Integer(5), new Integer(3), new Integer(5)},
        {"Jane", new Integer(5), new Integer(2), new Integer(5)},
        {"Joe", new Integer(5), new Integer(1), new Integer(5)}
        };

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
    }
    public static void main(String args[]){
        JFrame frame = new Frame();
        frame.setTitle("Choose an option");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

    }
}

【问题讨论】:

  • JPanel 切换后CardLayout/JTabbedPane 打包
  • 尝试发布 SSCCE (www.sscce.org)
  • JPanel 默认使用 FlowLayout,尝试使用 BorderLayout...
  • Andrew G:问题是代码中的每个面板,所以不知道如何简化...
  • 科贝尔先生,您能详细说明一下吗?

标签: java swing jtable jframe jpanel


【解决方案1】:

在您的actionPerformed 方法中添加

this.pack();

在方法结束时。

这将使其全部显示,但您可能需要考虑在面板中使用布局管理器来调整外观,即布局。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 2012-05-02
    相关资源
    最近更新 更多