Java界面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java界面相关的知识,希望对你有一定的参考价值。
JFrame.java
package myProject;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Font;
import java.io.IOException;
public class JFrame extends javax.swing.JFrame {
private JPanel contentPane;
private JTextArea textArea;
public static void main(String[] args) throws IOException{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrame() {
setTitle("Group5+");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100, 100, 800, 600);// frame size
contentPane = new JPanel();
contentPane.setLayout(null);
this.getContentPane().add(contentPane);//
//show buttons
controlPanel();
//show create test area
TextArea();
//show create map
Map();
//create hr
createLine();
}
public void Map(){
// map label
JLabel map = new JLabel("Map");
map.setFont(new Font("Arial Black", Font.PLAIN, 18));
map.setBounds(400, 80, 200, 20);
contentPane.add(map);
}
public void TextArea(){
//status testarea
JLabel status = new JLabel("Status");
status.setFont(new Font("Arial Black", Font.PLAIN, 18));
status.setBounds(20, 300, 200, 20);
contentPane.add(status);
}
public void createLine(){
JSeparator line1 = new JSeparator();
//position
line1.setBounds(15, 280, 330, 1);
//color
line1.setForeground(Color.black);
//add
contentPane.add(line1);
JSeparator infoLine = new JSeparator();
//position
infoLine.setBounds(15, 60, 700, 1);
//color
infoLine.setForeground(Color.black);
//add
contentPane.add(infoLine);
}
public void controlPanel(){
//control panel
JLabel controlPanel = new JLabel("Control Panel");
controlPanel.setFont(new Font("Arial Black", Font.PLAIN, 18));
controlPanel.setBounds(20, 80, 180, 20);
contentPane.add(controlPanel);
// button_up
Button button_up = new Button("添加",contentPane);
button_up.go();
button_up.setSize(150,120,80,35);
contentPane.add(button_up.getButton());
// button_down
Button button_down = new Button("查询/修改",contentPane);
button_down.go();
button_down.setSize(150,220,80,35);
contentPane.add(button_down.getButton());
// button_left
Button button_left = new Button("??",contentPane);
button_left.go();
button_left.setSize(50,173,80,35);
contentPane.add(button_left.getButton());
// button_right
Button button_right = new Button("?ú",contentPane);
button_right.go();
button_right.setSize(255,173,80,35);
contentPane.add(button_right.getButton());
}
}
Button.java
package myProject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Button {
private String content;
private JButton button;
JTextField textField;
private JPanel contentPane;
public Button(String content,JPanel contentPane) {
this.content = content;
this.button = new JButton(content);
this.contentPane = contentPane;
initialTextField();
}
public void initialTextField(){
textField = new JTextField();
textField.setBounds(50, 350, 300, 80);
contentPane.add(textField);
textField.setColumns(10);
}
public String getText() {
return content;
}
public JButton getButton() {
return button;
}
public void setSize(int x, int y, int width, int height) {
this.button.setBounds(x, y, width, height);
}
public void go() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// onclick function
switch (button.getText()) {
case "↑":
System.out.println("123");
break;
case "↓":
System.out.println("123");
break;
default:
System.out.println("button error");
break;
}
}
});
}
}
以上是关于Java界面的主要内容,如果未能解决你的问题,请参考以下文章