[英]How can i add two class that extends JPanel in One JFrame
I created two class that extends JPanel and want to add that two class in one Frame. 我创建了两个扩展JPanel的类,并希望在一个Frame中添加这两个类。 But i am unable to do it.
但是我做不到。 Anyone help please.
有人帮忙。
my classes are --> 我的课程是->
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CalButton extends JPanel {
private JButton[] buttons;
private static final String[] buttonNames = { "7", "8", "9", "/", "4", "5",
"6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
private JPanel buttonPanel;
public CalButton() {
// TODO Auto-generated constructor stub
buttons = new JButton[buttonNames.length];
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 3, 3));
for(int i=0; i<buttonNames.length; i++){
buttons[i] = new JButton(buttonNames[i]);
buttonPanel.add(buttons[i]);
}
}
}
Another Class--> 另一类->
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CalField extends JPanel {
private JPanel panelField;
private JTextField field;
public CalField() {
// TODO Auto-generated constructor stub
panelField = new JPanel();
panelField.setLayout(new GridLayout(1, 1,5,5));
field = new JTextField(20);
panelField.add(field);
}
}
Main class--> 主班->
public class Calculator {
public static void main(String[] args) {
JFrame application = new JFrame("Calculator");
CalField calField = new CalField();
CalButton calButton = new CalButton();
application.setLayout(new GridLayout(2, 1));
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(400, 450);
application.setVisible(true);
}
}
Anyone kindly solve this problem please. 请任何人解决这个问题。 i'm stuck with this problem.
我陷入了这个问题。
public CalButton() {
// TODO Auto-generated constructor stub
buttons = new JButton[buttonNames.length];
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 3, 3));
for(int i=0; i<buttonNames.length; i++){
buttons[i] = new JButton(buttonNames[i]);
buttonPanel.add(buttons[i]);
}
}
The CalButton
class already "is a" JPanel because you extend JPanel so there is no need to create another JPanel. CalButton
类已经“是” JPanel,因为扩展了JPanel,因此无需创建另一个JPanel。 Just add your buttons to the class: 只需将您的按钮添加到类中即可:
public CalButton() {
// TODO Auto-generated constructor stub
buttons = new JButton[buttonNames.length];
//buttonPanel = new JPanel();
//buttonPanel.setLayout(new GridLayout(4, 4, 3, 3));
setLayout(new GridLayout(4, 4, 3, 3));
for(int i=0; i<buttonNames.length; i++){
buttons[i] = new JButton(buttonNames[i]);
//buttonPanel.add(buttons[i]);
add(buttons[i]);
}
}
Same with your CalcField
class except in this case you probably don't need to set the layout manager. 与
CalcField
类相同,除了在这种情况下,您可能不需要设置布局管理器。 You can probably use the default FlowLayout. 您可能可以使用默认的FlowLayout。
Then the second problem is that you never add these panels to the frame: 然后第二个问题是您永远不会将这些面板添加到框架中:
CalField calField = new CalField();
CalButton calButton = new CalButton();
//application.setLayout(new GridLayout(2, 1));
application.add(calField, BorderLayout.PAGE_START);
application.add(calButton, BorderLayout.CENTER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.