java用户界面窗口

时间:2024-01-19 17:18:56

java用户界面窗口

窗口框

代码如下:

package Day08; import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
@SuppressWarnings("serial")
public class TestSwingCommonFeatures extends JFrame {
public TestSwingCommonFeatures() {
JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
JButton jbtLeft = new JButton("Left");
JButton jbtCenter = new JButton("Center");
JButton jbtRight = new JButton("Right");
jbtLeft.setBackground(Color.WHITE);
jbtCenter.setForeground(Color.GREEN);
jbtRight.setBackground(new Color(10, 100, 100));
jbtRight.setToolTipText("this is the right button");
jp1.add(jbtLeft);
jp1.add(jbtCenter);
jp1.add(jbtRight);
jp1.setBorder(new TitledBorder("3 buttons"));
Border lineBorder = new LineBorder(Color.BLACK, 2);
Font largeFont = new Font("黑体", Font.BOLD, 20);
JPanel jp2 = new JPanel(new GridLayout(1, 2, 5, 5));
JLabel lbl1 = new JLabel("Red");
JLabel lbl2 = new JLabel("Orange");
lbl1.setFont(largeFont);
lbl1.setForeground(Color.RED);
lbl1.setBorder(lineBorder);
lbl2.setForeground(Color.ORANGE);
lbl2.setBorder(lineBorder);
jp2.add(lbl1);
jp2.add(lbl2);
jp2.setBorder(new TitledBorder("2 labels"));

setLayout(new GridLayout(2,1,5,5));
add(jp1);
add(jp2);
}
public static void main(String[] args) {
TestSwingCommonFeatures frame = new TestSwingCommonFeatures();
frame.setTitle("TestSwingCommonFeatures");
frame.setSize(400, 300);//set the frame size
frame.setLocationRelativeTo(null);// center a frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// center a frame
frame.setVisible(true);//display the frame
}
}