我的相框:
public class Interface extends JFrame {
public void Interface(){
PannelDez panel2 = new PannelDez();
this.add(panel2);
this.setSize(400, 500);
this.setVisible(true);
this.setResizable(false);
// Menüleiste
JMenuBar jmb = new JMenuBar();
this.setJMenuBar(jmb);
JMenu file = new JMenu("Option");
jmb.add(file);
JMenuItem dez = new JMenuItem("Dezimal-Rechner");
file.add(dez);
JMenuItem hex = new JMenuItem("Hex-Rechner");
file.add(hex);
this.setVisible(true);
}
}
面板:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PannelDez extends JPanel {
JLabel text = new JLabel();
JButton delete = new JButton();
JButton zero = new JButton();
JButton eins = new JButton();
JButton zwei = new JButton();
JButton drei = new JButton();
JButton vier = new JButton();
JButton fuenf = new JButton();
JButton sechs = new JButton();
JButton sieben = new JButton();
JButton acht = new JButton();
JButton neun = new JButton();
JButton addieren = new JButton();
JButton subtrahieren = new JButton();
JButton multiplizieren = new JButton();
JButton dividieren = new JButton();
JButton ergebnis = new JButton();
JTextField anzeige = new JTextField();
String erg = "";
int temp1;
RechnerInt interfaceRechner = new RechnerInt();
public PannelDez() {
//Panel
JPanel panel2 = new JPanel();
panel2.setSize(400, 500);
this.setVisible(true);
anzeige.setBounds(125,100, 200, 50);
anzeige.setEditable(false);
anzeige.setBackground(Color.WHITE);
anzeige.setText(erg);
this.add(anzeige);
//Buttons
delete.setText("C");
zero.setText("0");
eins.setText("1");
zwei.setText("2");
drei.setText("3");
vier.setText("4");
fuenf.setText("5");
sechs.setText("6");
sieben.setText("7");
acht.setText("8");
neun.setText("9");
addieren.setText("+");
subtrahieren.setText("-");
multiplizieren.setText("*");
dividieren.setText("/");
ergebnis.setText("=");
delete.setBounds(225, 350, 50, 50);
zero.setBounds(175, 350,50,50);
eins.setBounds(125, 300,50,50);
zwei.setBounds(175, 300,50,50);
drei.setBounds(225, 300,50,50);
vier.setBounds(125, 250,50,50);
fuenf.setBounds(175, 250,50,50);
sechs.setBounds(225, 250,50,50);
sieben.setBounds(125, 200,50,50);
acht.setBounds(175, 200,50,50);
neun.setBounds(225, 200,50,50);
addieren.setBounds(275, 300,50,50);
subtrahieren.setBounds(275, 250,50,50);
multiplizieren.setBounds(275, 200,50,50);
dividieren.setBounds(275, 150,50,50);
ergebnis.setBounds(275, 350,50,50);
panel2.add(delete);
panel2.add(zero);
panel2.add(eins);
panel2.add(zwei);
panel2.add(drei);
panel2.add(vier);
panel2.add(fuenf);
panel2.add(sechs);
panel2.add(sieben);
panel2.add(acht);
panel2.add(neun);
panel2.add(addieren);
panel2.add(subtrahieren);
panel2.add(multiplizieren);
panel2.add(dividieren);
panel2.add(ergebnis);
delete.addActionListener(new ListenDelete());
zero.addActionListener(new ListenZero());
eins.addActionListener(new ListenEins());
zwei.addActionListener(new ListenZwei());
drei.addActionListener(new ListenDrei());
vier.addActionListener(new ListenVier());
fuenf.addActionListener(new ListenFuenf());
sechs.addActionListener(new ListenSechs());
sieben.addActionListener(new ListenSieben());
acht.addActionListener(new ListenAcht());
neun.addActionListener(new ListenNeun());
addieren.addActionListener(new ListenAddieren());
subtrahieren.addActionListener(new ListenSubtrahieren());
multiplizieren.addActionListener(new ListenMultiplizieren());
dividieren.addActionListener(new ListenDividieren());
ergebnis.addActionListener(new ListenErgebnis());
/* JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout()); */
}
class ListenDelete implements ActionListener {
public void actionPerformed (ActionEvent e) {
interfaceRechner.operators.clear();
interfaceRechner.numbers.clear();
interfaceRechner.numbersInt.clear();
anzeige.setText("");
}
}
class ListenZero implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "0");
erg = anzeige.getText();
}
}
class ListenEins implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "1");
erg = anzeige.getText();
return;
}
}
class ListenZwei implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "2");
erg = anzeige.getText();
}
}
class ListenDrei implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "3");
erg = anzeige.getText();
}
}
class ListenVier implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "4");
erg = anzeige.getText();
}
}
class ListenFuenf implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "5");
erg = anzeige.getText();
}
}
class ListenSechs implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "6");
erg = anzeige.getText();
}
}
class ListenSieben implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "7");
erg = anzeige.getText();
}
}
class ListenAcht implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "8");
erg = anzeige.getText();
}
}
class ListenNeun implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
anzeige.setText(erg + "9");
erg = anzeige.getText();
}
}
class ListenAddieren implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("+");
anzeige.setText("");
erg = null;
}
}
class ListenSubtrahieren implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("-");
anzeige.setText("");
erg = null;
}
}
class ListenMultiplizieren implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("*");
anzeige.setText("");
erg = null;
}
}
class ListenDividieren implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("/");
anzeige.setText("");
erg = null;
}
}
class ListenErgebnis implements ActionListener {
public void actionPerformed (ActionEvent e) {
erg = anzeige.getText();
interfaceRechner.addNumber(Integer.valueOf(erg));
interfaceRechner.addOperators("=");
anzeige.setText(""+interfaceRechner.berechneInt());
}
}
}
问题是:击打比赛时:
public class Main {
public static void main(String[] args) {
Interface interface1 =new Interface();
interface1.show();
}
}
没有出现任何面板。会弹出一个小的Java窗口,与我如何配置无关-直接在面板中或通过JFrame-它不会影响任何内容。现在有一些代码是多余的,我把它留在那里以记住我已经尝试过的内容。但是我似乎找不到合适的解决方案。
打开的窗口始终为空,那里没有按钮,顶部没有菜单栏。
答案 0 :(得分:1)
这里:
public void Interface(){
那不是不是构造函数!
您需要
public Interface() {
相反。除此之外:使用有意义的名称。 “接口”没有说明其背后的组件。为什么不叫它GuiFrame或类似的东西?