package rom;
import java.lang.*;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.plaf.BorderUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class Xamle_4 extends JFrame{
public Xamle_4(String name) {
JFrame jf =new JFrame(name);
Container container = jf.getContentPane();
jf.setLayout(null);
//container.setLayout(new GridLayout(2, 1,10,10)); //设置布局方式
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
String [] strings = {"菠萝","香蕉","苹果","哈密瓜","西瓜"}; //创建下拉菜单栏的信息
JComboBox<String> jComboBox = new JComboBox<>(strings); //创建下拉菜单栏
jComboBox.addItemListener(new ItemListener() { //点击下拉多选菜单的监控
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange()==ItemEvent.SELECTED) { //选择点击
System.out.println("你选择的是:");
}
if(jComboBox.getSelectedItem().equals("菠萝")) {
System.out.println("菠萝");
}
if(jComboBox.getSelectedItem().equals("香蕉")) {
System.out.println("香蕉");
}
if(jComboBox.getSelectedItem().equals("苹果")) {
System.out.println("苹果");
}
if(jComboBox.getSelectedItem().equals("哈密瓜")) {
System.out.println("哈密瓜");
}
if(jComboBox.getSelectedItem().equals("西瓜")) {
System.out.println("西瓜");
}
// TODO Auto-generated method stub
}
});
JRadioButton jr1 = new JRadioButton("按键1"); //添加单选按键
JRadioButton jr2 = new JRadioButton("按键2"); //添加单选按键
// p1.setPreferredSize(new Dimension(150, 150)); //设置Jpanel 首选大小 ,Dimension是尺子工具的意思 面板大小
// p2.setPreferredSize(new Dimension(150, 150)); //设置JPanel 面板大小
ButtonGroup group = new ButtonGroup(); //创建单选按键集合
p1.setBorder(BorderFactory.createTitledBorder("选择框边框")); //设置P1面板的边界显示方式,此方式为标题边框 //BorderFactory 是边界工具的意思
p2.setBorder(BorderFactory.createEmptyBorder()); //创建空白边框
p3.setBorder(BorderFactory.createLineBorder(Color.BLUE)); //创建线边框 (选择蓝色)
p4.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); //创建凸起效果边框
p5.setBorder(BorderFactory.createMatteBorder(20, 20, 20, 20, Color.yellow)); //创建带厚度和颜色的边框
p6.setBorder(BorderFactory.createSoftBevelBorder(BevelBorder.LOWERED)); //创建带凹面效果的边框
p1.setBounds(1, 1, 200, 200); //设置p1 面板的位置和大小
p2.setBounds(201, 1, 200, 200); //设置p2 面板的位置和大小
p3.setBounds(402, 1, 200, 200);
p4.setBounds(1, 201, 200, 200);
p5.setBounds(201, 201, 200, 200);
p6.setBounds(402, 201, 200, 200);
group.add(jr1); //将单选按钮添加到一个组合中
group.add(jr2);
p1.add(jr1);
p1.add(jr2);
p2.add(new JButton("1"));//创建一个按钮
p3.add(new JButton("2"));
p4.add(new JButton("3"));
p5.add(jComboBox); //将下拉菜单添加到面板里
container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
container.add(p5);
container.add(p6);
jf.setVisible(true); //将框体可视
jf.setSize(630, 500); //宽 和 高
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //设置关闭方式
}
public static void main(String [] agrs) {
new Xamle_4("单选按键测试");
}
}