jframe制作留言板
时间: 2023-07-04 22:10:48 浏览: 41
好的,以下是一个简单的JFrame留言板制作过程:
1. 新建一个Java项目,并创建一个JFrame窗口。
2. 在JFrame窗口中添加一个JPanel,用于放置留言板的组件。
3. 在JPanel中添加一个JLabel,用于显示留言板的标题。
4. 在JPanel中添加一个JTextArea,用于输入留言内容。
5. 在JPanel中添加一个JButton,用于提交留言。
6. 在JPanel中添加一个JScrollPane,用于显示留言内容,如果超过JTextArea的大小,可以滚动查看。
7. 在JButton上添加一个ActionListener,用于提交留言。
8. 在ActionListener中,获取JTextArea中的文本内容,并将其添加到JScrollPane中。
9. 运行程序,测试留言板功能。
这是一个简单的留言板制作过程,具体的实现可以根据需要进行修改和扩展。
相关问题
jframe制作计算器
好的,以下是一个简单的JFrame计算器示例代码,你可以根据自己的需要进行修改:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton[] buttons;
private String[] buttonText = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", ".", "0", "=", "/"};
private JPanel panel;
public Calculator() {
setTitle("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
textField = new JTextField(10);
textField.setEditable(false);
textField.setFont(new Font("Arial", Font.PLAIN, 20));
textField.setHorizontalAlignment(JTextField.RIGHT);
buttons = new JButton[buttonText.length];
for (int i = 0; i < buttonText.length; i++) {
buttons[i] = new JButton(buttonText[i]);
buttons[i].setFont(new Font("Arial", Font.PLAIN, 20));
buttons[i].addActionListener(this);
}
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 5, 5));
for (JButton button : buttons) {
panel.add(button);
}
getContentPane().add(textField, BorderLayout.NORTH);
getContentPane().add(panel, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("=")) {
String expression = textField.getText();
textField.setText(String.valueOf(CalculatorEngine.evaluate(expression)));
} else {
textField.setText(textField.getText() + command);
}
}
public static void main(String[] args) {
new Calculator();
}
}
class CalculatorEngine {
public static double evaluate(String expression) {
//使用JavaScript引擎计算表达式
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
return (double) engine.eval(expression);
} catch (ScriptException e) {
e.printStackTrace();
return Double.NaN;
}
}
}
```
这个计算器使用了 GridLayout 来布局按钮,使用 JavaScript 引擎来计算表达式。
jframe给面板设置背景图片
可以使用如下代码设置 JFrame 面板的背景图片:
```java
JFrame frame = new JFrame("My JFrame");
JPanel panel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
// 加载背景图片
Image bg = ImageIO.read(new File("background.jpg"));
// 绘制背景图片
g.drawImage(bg, 0, 0, getWidth(), getHeight(), null);
} catch (IOException e) {
e.printStackTrace();
}
}
};
frame.add(panel);
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
```
其中,`JPanel` 继承自 `JComponent`,可以使用 `paintComponent` 方法来绘制自己的界面,我们可以在该方法中加载背景图片,并将其绘制出来。