3.Swing
3.1.窗口
public class JFrameDemo {
public static void main(String[] args) {
new MyJFrame();
}
}
class MyJFrame extends JFrame {
public MyJFrame(){
setBounds(200,200,200,200);
setVisible(true);
JLabel jLabel = new JLabel("欢迎你");
add(jLabel);
//让文本标签居中 设置水平对齐
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// setBackground(Color.BLUE);在JFrame中不行
//获得一个容器
Container container = getContentPane();
container.setBackground(Color.black);
// 关闭事件
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
3.2.弹窗
public class MyDialog extends JFrame {
public void init(){
setVisible(true);
setBounds(200,200,200,200);
Container container = getContentPane();
container.setBackground(Color.BLUE);
JButton jButton = new JButton("按钮");
add(jButton);
//点这个按钮的时候,弹出一个窗口
jButton.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
new MyJDialog();
}
});
}
//弹出的窗口
private class MyJDialog extends JDialog{
public MyJDialog(){
setVisible(true);
setBounds(200,200,200,200);
Container contentPane = getContentPane();
contentPane.setBackground(Color.BLACK);
}
}
public static void main(String[] args) {
new MyDialog().init();
}
}
3.3.标签
label
new JLabel("xxx");
图标icon
//图标接口,需要实现类,JFrame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int height;
public IconDemo(){}
public IconDemo(int width, int height){
this.width = width;
this.height = height;
}
public void init(){
IconDemo iconDemo = new IconDemo(15,15);
//图标放在标签上,也可以放在按钮上
JLabel label = new JLabel("a", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
图片icon
public class IconDemo1 extends JFrame {
public static void main(String[] args) {
new IconDemo1();
}
public IconDemo1(){
JLabel jLabel = new JLabel("abc");
URL url = IconDemo1.class.getResource("1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
int i =1;
jLabel.setIcon(imageIcon);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setBounds(200,200,200,200);
}
}
3.4.面板
public class JPanelTest extends JFrame{
public static void main(String[] args) {
new JPanelTest();
}
public JPanelTest(){
JPanel jPanel = new JPanel();
JButton jButton = new JButton("我是一个按钮");
jPanel.add(jButton);
Container contentPane = getContentPane();
contentPane.add(jPanel);
setBounds(200,200,200,200);
setVisible(true);
}
}
public class JTextTest extends JFrame {
public JTextTest(){
Container container = getContentPane();
//文本域
JTextArea area = new JTextArea(10,10);
area.setText("hello");
//Scroll下拉面板
JScrollPane pane = new JScrollPane(area);
//pane.add(area);这样不行
container.add(pane);
setBounds(200,200,200,200);
setVisible(true);
}
public static void main(String[] args) {
new JTextTest();
}
}
3.5.按钮
图片按钮
public class TestIconButton extends JFrame {
public TestIconButton(){
Container container = getContentPane();
JButton button = new JButton();
URL url = TestIconButton.class.getResource("1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
button.setIcon(imageIcon);
container.add(button);
setVisible(true);
setBounds(200,200,200,200);
}
public static void main(String[] args) {
new TestIconButton();
}
}
单选按钮
public class TestIconButton extends JFrame {
public TestIconButton(){
Container container = getContentPane();
//单选按钮
JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton();
//由于单选框只能选择一个,分组,一个组中只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.SOUTH);
setVisible(true);
setBounds(200,200,200,200);
}
public static void main(String[] args) {
new TestIconButton();
}
}
多选按钮
public class TestIconButton extends JFrame {
public TestIconButton(){
Container container = getContentPane();
container.setLayout(new FlowLayout());
JCheckBox jCheckBox1 = new JCheckBox();
JCheckBox jCheckBox2 = new JCheckBox();
container.add(jCheckBox1);
container.add(jCheckBox2);
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestIconButton();
}
}
3.6.列表
下拉列表
public class TestComboBox extends JFrame {
public static void main(String[] args) {
new TestComboBox();
}
public TestComboBox(){
Container container = getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem("准备上映");
comboBox.addItem("已经上映");
comboBox.addItem("上映完毕");
container.add(comboBox);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(200,200,200,200);
}
}
列表框
public class TestJlist extends JFrame {
public TestJlist(){
Container container = getContentPane();
//生成列表的内容
String[] sr = {"1","2","3"};
//列表中需要放入内容
JList list = new JList(sr);
container.add(list);
setVisible(true);
setBounds(200,200,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestJlist();
}
}
3.7.文本框
public class TestText extends JFrame {
public static void main(String[] args) {
new TestText();
}
public TestComboBox(){
Container container = getContentPane();
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
container.add(textField1,BorderLayout.NORTH);
container.add(textField2,BorderLayout.SOUTH);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(200,200,200,200);
}
}
public class JTextTest extends JFrame {
public JTextTest(){
Container container = getContentPane();
//文本域
JTextArea area = new JTextArea(10,10);
area.setText("hello");
//Scroll下拉面板
JScrollPane pane = new JScrollPane(area);
//pane.add(area);这样不行
container.add(pane);
setBounds(200,200,200,200);
setVisible(true);
}
public static void main(String[] args) {
new JTextTest();
}
}
3.8.密码框
public class TestComboBox extends JFrame {
public static void main(String[] args) {
new TestPW();
}
public TestPW(){
Container container = getContentPane();
JPasswordField passwordField = new JPasswordField();
//密码框默认为..... 也可通过passwordField.setEchoChar('*');手动设置为*
container.add(passwordField);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setBounds(200,200,200,200);
}
}
转载:https://blog.csdn.net/weixin_45363806/article/details/105916610
查看评论