我有这个代码我一直在努力让这些面板分开。我希望屏幕上有一个带有文本区域的面板,另一个用于按钮。我需要帮助,我被困在某个地方。我想确保我生产一个类似手机的界面
谢谢
这就是我所做的
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);
}
}
答案 0 :(得分:3)
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);
2然后你将jsp1和jsp2添加到内容窗格中,这是不行的。组件只能有一个父组件。如果希望面板位于拆分窗格中,则只需将它们添加到拆分窗格中。
//phone.show();
phone.setVisible(true);
不要使用不推荐使用的show()方法。相反,你应该使用setVisible()方法。
阅读How to Use Split Panes上Swing教程中的部分,了解更多信息和工作示例。