RRROLE 发表于 2021-5-23 22:44:50

GUI创建窗口

做成如图所示的效果,贴上自己写的全是bug的代码,实在找不出来了package Test_1;

import javax.swing.*;
import java.awt.*;

public class Register extends JFrame {
    JLabel lb1,lb2,lb3,lb4,lb5,lb6,lb7;
    JTextField jtf1,jtf2,jtf3,jtf4;
    JRadioButton radioM,radioF;//单选框
    JPasswordField jpf1,jpf2;
    JButton jb1,jb2;
    ButtonGroup group;

    JCheckBox checkBox1,checkBox2,checkBox3;//复选框

    JComboBox<String> comBox;//下拉列表

    public Register(){

      init();
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    void init(){
      setLayout(new FlowLayout());


       lb1=new JLabel("账号");
       add(lb1);

       jtf1=new JTextField(10);
       add(jtf1);

       lb2=new JLabel("密码");
       add(lb2);

       jpf1=new JPasswordField(10);
       add(jpf1);

       lb3=new JLabel("再次密码");
       add(lb3);

       jpf2=new JPasswordField(10);
       add(jpf2);

       lb4=new JLabel("性别");
       add(lb4);

       group =new ButtonGroup();
       radioM=new JRadioButton("男");
       radioF=new JRadioButton("女");

       group.add(radioF);
       group.add(radioM);

       lb5=new JLabel("学历");
       comBox =new JComboBox<String>();
      comBox.addItem("初中");
      comBox.addItem("高中");
      comBox.addItem("大学");
      add(comBox);

      lb6=new JLabel("爱好");
      add(lb6);

      checkBox1=new JCheckBox("体育");
      checkBox1=new JCheckBox("音乐");
      checkBox1=new JCheckBox("游戏");
      add(checkBox1);
      add(checkBox2);
      add(checkBox3);
//      checkBox1=

      lb7=new JLabel("简介");
      add(lb7);
      jtf2=new JTextField(30);
      JScrollPane jtf21=new JScrollPane(jtf2);//滚动面板
      jtf21.setHorizontalScrollBarPolicy(jtf21.HORIZONTAL_SCROLLBAR_ALWAYS);//设置水平方向滚动条。
      jtf21.setVerticalScrollBarPolicy(jtf21.VERTICAL_SCROLLBAR_ALWAYS);//总是显示垂直方向滚动条。

      jb1=new JButton("注册");
      jb2=new JButton("查看");
    }

    public static void main(String[] args) {
       Register r1=new Register();
       r1.setBounds(100,100,450,260);
       r1.setTitle("Register");
    }
}

RRROLE 发表于 2021-5-23 22:45:29

乖巧{:10_281:}

RRROLE 发表于 2021-5-23 22:48:05

水一下{:10_257:}

wuliangtdi 发表于 2021-5-23 23:36:57

第六十七到第六十八行 另外建议不用swing 太丑了 虽然说自己可以美化 但是蛮麻烦的 建议可以学学JavaFx 蛮不错的

连帅帅 发表于 2021-5-25 11:47:05

本帖最后由 连帅帅 于 2021-5-25 11:49 编辑

最近也在搞这笔玩意(公司让用的),都淘汰的东西了,搞不懂为什么还在有人用{:5_99:}

hornwong 发表于 2021-5-25 12:01:50

感谢分享!

连帅帅 发表于 2021-5-25 13:39:46

还是回复一下子吧,按你的页面布局自己写的,能用就用,不能用就参考吧。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* TestJFrame.java
*
* Created on 2021-5-20, 14:49:33
*/

package com.gc.nb.gui;

/**
*
* @author Administrator
*/
public class TestJFrame extends javax.swing.JFrame {

    /** Creates new form TestJFrame */
    public TestJFrame() {
      initComponents();
    }

    /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

      buttonGroup1 = new javax.swing.ButtonGroup();
      jPanel1 = new javax.swing.JPanel();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();
      jLabel4 = new javax.swing.JLabel();
      jRadioButton1 = new javax.swing.JRadioButton();
      jRadioButton2 = new javax.swing.JRadioButton();
      jLabel5 = new javax.swing.JLabel();
      jComboBox1 = new javax.swing.JComboBox();
      jLabel6 = new javax.swing.JLabel();
      jCheckBox1 = new javax.swing.JCheckBox();
      jCheckBox2 = new javax.swing.JCheckBox();
      jCheckBox3 = new javax.swing.JCheckBox();
      jLabel7 = new javax.swing.JLabel();
      textArea1 = new java.awt.TextArea();
      jButton1 = new javax.swing.JButton();
      jButton2 = new javax.swing.JButton();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jPanel1.setName("jPanel1"); // NOI18N

      jLabel1.setText("用户名:");
      jLabel1.setName("jLabel1"); // NOI18N

      jLabel2.setText("密码:");
      jLabel2.setName("jLabel2"); // NOI18N

      jLabel3.setText("再次密码:");
      jLabel3.setName("jLabel3"); // NOI18N

      jLabel4.setText("性别");
      jLabel4.setName("jLabel4"); // NOI18N

      jRadioButton1.setText("男");
      jRadioButton1.setName("jRadioButton1"); // NOI18N

      jRadioButton2.setText("女");
      jRadioButton2.setName("jRadioButton2"); // NOI18N

      jLabel5.setText("学历:");
      jLabel5.setName("jLabel5"); // NOI18N

      jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "大学", "高中", "初中", "小学" }));
      jComboBox1.setName("jComboBox1"); // NOI18N

      jLabel6.setText("爱好:");
      jLabel6.setName("jLabel6"); // NOI18N

      jCheckBox1.setText("体育");
      jCheckBox1.setName("jCheckBox1"); // NOI18N

      jCheckBox2.setText("音乐");
      jCheckBox2.setName("jCheckBox2"); // NOI18N

      jCheckBox3.setText("游戏");
      jCheckBox3.setName("jCheckBox3"); // NOI18N

      jLabel7.setText("简介:");
      jLabel7.setName("jLabel7"); // NOI18N

      textArea1.setName("textArea1"); // NOI18N

      jButton1.setText("注册");
      jButton1.setName("jButton1"); // NOI18N

      jButton2.setText("查看");
      jButton2.setName("jButton2"); // NOI18N

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                        .addComponent(jButton2))
                  .addComponent(jLabel1)
                  .addComponent(jLabel2)
                  .addComponent(jLabel3)
                  .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                              .addComponent(jRadioButton1)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                              .addComponent(jRadioButton2))))
                  .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                              .addComponent(jLabel7)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                              .addComponent(textArea1, 0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                              .addComponent(jLabel6)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                              .addComponent(jCheckBox1)
                              .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                              .addComponent(jCheckBox2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCheckBox3)))
                .addContainerGap(24, Short.MAX_VALUE))
      );
      jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                  .addComponent(jLabel4)
                  .addComponent(jRadioButton1)
                  .addComponent(jRadioButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                  .addComponent(jLabel5)
                  .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                  .addComponent(jLabel6)
                  .addComponent(jCheckBox1)
                  .addComponent(jCheckBox2)
                  .addComponent(jCheckBox3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(jLabel7)
                  .addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                  .addComponent(jButton1)
                  .addComponent(jButton2))
                .addContainerGap(161, Short.MAX_VALUE))
      );

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );
      layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );

      pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestJFrame().setVisible(true);
            }
      });
    }

    // Variables declaration - do not modify
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private java.awt.TextArea textArea1;
    // End of variables declaration

}

qq1151985918 发表于 2021-5-28 08:20:37

原来java作这么一个界面这么复杂吗,感觉python做出来没什么难度

不会起名字的我 发表于 2021-5-29 09:14:47

连帅帅 发表于 2021-5-25 11:47
最近也在搞这笔玩意(公司让用的),都淘汰的东西了,搞不懂为什么还在有人用

能跨平台{:10_256:}
页: [1]
查看完整版本: GUI创建窗口