簡體   English   中英

Java Swing JComboBox / Button編輯參數

[英]Java Swing JComboBox/ Button edit parameters

我對使用Java中的GUI很陌生。 我正在嘗試幾種方法,例如,在run方法中使用以下命令將新條目添加到JComboBox或更改JButtons標題:

pwSelection.addItem(“ Name 1”);

dec_btn.setText(“ Example”);

受保護的JComboBox pwSelection =新的JComboBox(內容);

不幸的是,我啟動程序后,沒有任何效果。 布局是使用IntelliJ GUI Form Creator創建的。 如果您能給我任何提示或替代方法,那將是很棒的。

package PackMain;

import javax.swing.*;

public class Password {

    String[] contents = {"Name 1", "Name 2"};
    protected JTextField newAdress;
    protected JPanel panel1;
    protected JComboBox pwSelection = new JComboBox(contents); // ?
    protected JLabel title;
    protected JTextField pwOutput;
    protected JButton enc_btn;
    protected JButton dec_btn;

    public JFrame run(){
        JFrame mainFrame= new JFrame ("Password");
        mainFrame.setContentPane(new Password().panel1);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.pack();
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setSize(400, 200);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);

        pwSelection.addItem("Name 1");
        dec_btn.setText("Example");

        return mainFrame;
    }


    public static void main(String[] args){
        new Password().run();
    }

}

panel1從未被定義為新的JPanel(),因此它為null,並且從未將所有其他組件(pwSelection,title等)添加到panel1中。

您沒有在框架中添加任何面板,也沒有初始化任何屬性...

package password;

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

public class Password extends JFrame{

String[] contents = {"Name 1", "Name 2"};
protected JTextField newAdress;
protected JPanel panel1, panel2; //2 panels for a more organized approach...
protected JComboBox  pwSelection;// ?
protected JTextField pwOutput;
protected JButton enc_btn;
protected JButton dec_btn;

public Password(){          //Constructor of the class, initialize stuff here...

    super("Shinny title");
    //1. Initialize all your atributtes...
    newAdress = new JTextField();
    panel1 = new JPanel();
    panel2 = new JPanel();
    pwSelection = new JComboBox(contents);
    pwOutput= new JTextField();
    enc_btn = new JButton("First button");
    dec_btn = new JButton("Second Button");
    //Set layouts..
    panel1.setLayout(new GridLayout(3,1));          
panel2.setLayout(new FlowLayout());

    //Add items to panel 1...
    panel1.add(new JLabel("Adress:"));  //A more simple aproach for creating a label...
    panel1.add(newAdress);
    panel1.add(pwSelection);
    panel1.add(enc_btn);
    panel1.add(dec_btn);
    panel1.add(pwOutput);
    //Add items of panel 1 to panel 2...
    panel2.add(panel1);
    add(panel2);

    setSize(300,300);
    setVisible(true);

    //Proper way of stop the application at closing...
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

//This is not necessary as everything should be implemented in the constructor.
/*
public JFrame run(){
    JFrame mainFrame= new JFrame ("Password");
    mainFrame.setContentPane(new Password().panel1);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.pack();
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setSize(400, 200);
    mainFrame.setResizable(false);
    mainFrame.setVisible(true);

    pwSelection.addItem("Name 1");
    dec_btn.setText("Example");

    setVisible(true);
    return mainFrame;
}

* /

public static void main(String[] args){
    //Initialize the constructor...
    Password ps = new Password();
}

}

這將使您了解如何在Java上實現GUI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM