JLabel和JTextField为什么不用申请Jpanel
时间: 2024-02-02 12:04:35 浏览: 90
JLabel和JTextField是Java Swing组件库中的组件,它们都是直接继承自JComponent,而不是继承自JPanel。因此,使用JLabel和JTextField时不需要先申请JPanel,可以直接将它们添加到任何容器中,如JFrame或JDialog等。但是,如果需要将多个组件组合在一起,可以使用JPanel作为容器来布局它们。
相关问题
编写程序,利用常用组件JButton,Jlabel,JtextField等组件实现如下所示的计算器。
好的,以下是一个简单的计算器GUI程序的代码实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorGUI extends JFrame implements ActionListener {
// 定义计算器界面中的组件
private JTextField inputField;
private JLabel resultLabel;
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton buttonPlus;
private JButton buttonMinus;
private JButton buttonMultiply;
private JButton buttonDivide;
private JButton buttonEqual;
private JButton buttonClear;
// 定义计算器操作数和运算符变量
private double num1 = 0;
private double num2 = 0;
private String operator = "";
public CalculatorGUI() {
// 初始化计算器界面
setTitle("Calculator");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
inputField = new JTextField(12);
inputField.setEditable(false);
inputField.setHorizontalAlignment(JTextField.RIGHT);
resultLabel = new JLabel("0");
resultLabel.setHorizontalAlignment(JLabel.RIGHT);
button0 = new JButton("0");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
buttonPlus = new JButton("+");
buttonMinus = new JButton("-");
buttonMultiply = new JButton("*");
buttonDivide = new JButton("/");
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
// 设置计算器界面布局
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(2, 1));
inputPanel.add(inputField);
inputPanel.add(resultLabel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
buttonPanel.add(buttonPlus);
buttonPanel.add(button4);
buttonPanel.add(button5);
buttonPanel.add(button6);
buttonPanel.add(buttonMinus);
buttonPanel.add(button7);
buttonPanel.add(button8);
buttonPanel.add(button9);
buttonPanel.add(buttonMultiply);
buttonPanel.add(buttonClear);
buttonPanel.add(button0);
buttonPanel.add(buttonEqual);
buttonPanel.add(buttonDivide);
Container contentPane = getContentPane();
contentPane.add(inputPanel, BorderLayout.NORTH);
contentPane.add(buttonPanel, BorderLayout.CENTER);
// 绑定事件监听器
button0.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
buttonPlus.addActionListener(this);
buttonMinus.addActionListener(this);
buttonMultiply.addActionListener(this);
buttonDivide.addActionListener(this);
buttonEqual.addActionListener(this);
buttonClear.addActionListener(this);
pack();
}
public void actionPerformed(ActionEvent e) {
// 处理按钮点击事件
String action = e.getActionCommand();
if (action.equals("C")) {
inputField.setText("");
resultLabel.setText("0");
num1 = 0;
num2 = 0;
operator = "";
} else if (action.equals("+")) {
num1 = Double.parseDouble(inputField.getText());
inputField.setText("");
operator = "+";
} else if (action.equals("-")) {
num1 = Double.parseDouble(inputField.getText());
inputField.setText("");
operator = "-";
} else if (action.equals("*")) {
num1 = Double.parseDouble(inputField.getText());
inputField.setText("");
operator = "*";
} else if (action.equals("/")) {
num1 = Double.parseDouble(inputField.getText());
inputField.setText("");
operator = "/";
} else if (action.equals("=")) {
num2 = Double.parseDouble(inputField.getText());
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
resultLabel.setText(String.valueOf(result));
inputField.setText("");
num1 = 0;
num2 = 0;
operator = "";
} else {
inputField.setText(inputField.getText() + action);
}
}
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
calculator.setVisible(true);
}
}
```
运行以上代码后,便可以看到一个简单的计算器界面。
import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.text.DecimalFormat; import java.util.regex.*; public class WeightIndex extends JFrame { private static final long serialVersionUID = 1L; private JPanel contentPane; private JLabel titleLabel; private JPanel contentPanel; private JButton submitButton; private ButtonGroup bg; private JPanel sexPanel; private JRadioButton ChinaRadio; private JRadioButton AsiaRadio; private JRadioButton WHORadio; private JPanel whPanel; private JLabel heightLabel; private JLabel weightLabel; private JTextField heightText; private JTextField weightText; private JPanel consolePanel; private JLabel consoleLabel; private JTextField consoleText; 分析一下这段代码
这段代码定义了一个名为`WeightIndex`的类,继承自`JFrame`,表示一个窗口应用程序。其中,`serialVersionUID`是一个用于序列化的静态变量。
类中包含了一系列组件,如`contentPane`、`titleLabel`、`contentPanel`、`submitButton`、`bg`、`sexPanel`、`ChinaRadio`、`AsiaRadio`、`WHORadio`、`whPanel`、`heightLabel`、`weightLabel`、`heightText`、`weightText`、`consolePanel`、`consoleLabel`、`consoleText`,它们用于创建布局和显示UI界面。
在类中使用了Java的AWT和Swing库,其中包括了`JFrame`、`JPanel`、`JLabel`、`JButton`、`ButtonGroup`、`JRadioButton`、`JTextField`等组件,用于构建UI界面。
对于代码中的`JRadioButton`,通过将它们添加到同一个`ButtonGroup`中,可以实现单选按钮的互斥。
整个类的作用是实现一个体重指数计算器的UI界面,用户可以在界面上输入身高和体重数据,选择计算方法,然后通过点击提交按钮计算体重指数并显示结果。
阅读全文