【问题标题】:Splitting panes for screen and battons拆分屏幕和按钮的窗格
【发布时间】:2015-05-21 14:44:22
【问题描述】:

我有这段代码,我一直在努力尝试拆分这些面板。我想获得一个带有文本区域的屏幕面板和另一个用于按钮的面板。我需要帮助,我在某个地方卡住了。我想确保我制作了一个类似电话的界面

谢谢

这是我所做的

import java.awt.*;
import javax.swing.*;
class Phone {

    public static void main(String[] args) {

        JFrame phone = new JFrame("My First Gui");
        phone.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel jsp1 = new JPanel();
        JPanel jsp2 = new JPanel();

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, jsp1, jsp2);

        // splitPane.setOneTouchExpandable(true);
        getContentPane().add(splitPane);

        jsp1.add(new James());
        jsp2.add(new Doris());

        phone.getContentPane().add(jsp1);
        phone.getContentPane().add(jsp2);

        phone.pack();
        phone.show();

    }
}

class James extends JPanel {

    public James() {

        BorderLayout bb = new BorderLayout();
        setLayout(bb);

        JLabel txt1 = new JLabel("Phone ");
        JTextArea tx1 = new JTextArea(300, 100);

        add(tx1, bb.CENTER);
        add(txt1, bb.NORTH);
        // add(txt1);
        // add(txt2);
        // add(b1);


    }

}

class Doris extends JPanel {

    public Doris() {

        GridLayout grd = new GridLayout(4, 3, 2, 2);
        setLayout(grd);

        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bs = new JButton("*");
        JButton b0 = new JButton("0");
        JButton bt = new JButton("#");

        add(b1);
        add(b2);
        add(b3);
        add(b4);
        add(b5);
        add(b6);
        add(b7);
        add(b8);
        add(b9);
        add(bs);
        add(b0);
        add(bt);

    }

}

【问题讨论】:

    标签: java swing jframe jpanel awt


    【解决方案1】:
        JPanel jsp1 = new JPanel();
        JPanel jsp2 = new JPanel();
    
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, jsp1, jsp2);
    
        // splitPane.setOneTouchExpandable(true);
        getContentPane().add(splitPane);
    
        jsp1.add(new James());
        jsp2.add(new Doris());
    
        //phone.getContentPane().add(jsp1);
        //phone.getContentPane().add(jsp2);
    
    1. 首先将 jsp1 和 jsp2 添加到拆分窗格中,这没关系。然后将拆分窗格添加到内容窗格中,这也可以。

    2 但是随后您将 jsp1 和 jsp2 添加到内容窗格中,这是不行的。一个组件只能有一个父级。如果您希望面板位于拆分窗格中,只需将它们添加到拆分窗格即可。

    //phone.show();
    phone.setVisible(true);
    

    不要使用它已被弃用的 show() 方法。相反,您应该使用 setVisible() 方法。

    阅读 How to Use Split Panes 上的 Swing 教程部分,了解更多信息和工作示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-04
      • 2019-12-16
      • 2014-12-27
      • 1970-01-01
      相关资源
      最近更新 更多