如何将文本从TextField,复选框,RadioButton,ComboBox加载到一个大文本字段

时间:2018-01-25 12:57:22

标签: java checkbox

我正在编写一个招聘员工的简单程序。

我对JtextField几乎没有问题,很少有文字复选框,带文字的RadioButton和一个Combobox。

我需要在文本窗口,复选框,下拉列表中输入的数据被抛入右侧的大窗口。 目前只有来自文本窗口的数据,但不是来自场地中间的顶部。

package appka;
import javax.swing.*;

import java.awt.Checkbox;
import java.awt.event.*;

public class CzwartaApp {

    public static void main(String[] args) {
        // Klasy obiektow
        JFrame frame = new JFrame("Czwarta Aplikacja");

        JButton btnSubmit = new JButton("Wypisz");
        JButton btnExit = new JButton("Wyjdz");

        JLabel lbl1 = new JLabel("imię");
        JLabel lbl2 = new JLabel("nazwisko");
        JLabel lbl3 = new JLabel("stanowisko");
        JLabel lbl4 = new JLabel("E-mail");
        JLabel lbl5 = new JLabel("");
        JLabel lbl6 = new JLabel("Jakie znasz języki programowania?");
        JLabel lbl7 = new JLabel("Wybierz poziom języka angielskiego?");
        JLabel lbl8 = new JLabel("Wybierz kurs programowania?");
        JLabel lbl9 = new JLabel("Dane Kontaktowe");

        JTextField tf1 = new JTextField();
        JTextField tf2 = new JTextField();
        JTextField tf3 = new JTextField();
        JTextField tf4 = new JTextField();
        JTextField tf5 = new JTextField();

        Checkbox checkbox1 = new Checkbox("Java");
        Checkbox checkbox2 = new Checkbox("Python");
        Checkbox checkbox3 = new Checkbox("Inne");

        JRadioButton btn1 = new JRadioButton();
        btn1.setText("podstawowy");
        JRadioButton btn2 = new JRadioButton();
        btn2.setText("średnio-zaawansowany");
        JRadioButton btn3 = new JRadioButton();
        btn3.setText("zaawansowany");
        ButtonGroup group = new ButtonGroup ();
        group.add(btn1); group.add(btn2); group.add(btn3);

        JComboBox kurs = new JComboBox();
        kurs.addItem("FrontEnd Developer");
        kurs.addItem("BackEnd Developer");

        // Pozycjonowanie
        lbl1.setBounds(20, 20, 100, 20);
        tf1.setBounds(20, 70, 100, 20);
        lbl2.setBounds(20, 120, 100, 20);
        tf2.setBounds(20, 170, 100, 20);
        lbl3.setBounds(20, 220, 100, 20);
        tf3.setBounds(20, 270, 100, 20);
        lbl4.setBounds(20, 320, 100, 20);
        tf4.setBounds(20, 370, 100, 20);
        tf5.setBounds(500, 20, 250, 300);
        lbl5.setBounds(20, 420, 300, 20);
        lbl6.setBounds(200, 20, 250, 20);
        lbl7.setBounds(200, 220, 250, 20);
        lbl8.setBounds(200, 350, 250, 20);
        lbl9.setBounds(500, 50, 200, 20);
        btnSubmit.setBounds(20, 470, 100, 20);
        btnExit.setBounds(220, 470, 100, 20);
        checkbox1.setBounds(200, 70, 250, 20);
        checkbox2.setBounds(200, 120, 250, 20);
        checkbox3.setBounds(200, 170, 250, 20);
        btn1.setBounds(200, 250, 250, 20);
        btn2.setBounds(200, 280, 250, 20);
        btn3.setBounds(200, 310, 250, 20);
        kurs.setBounds(200, 380, 250, 20);


        // Obsluga zdarzen
        btnSubmit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                String imie = tf1.getText();
                String nazwisko = tf2.getText();
                String stanowisko = tf3.getText();
                String email = tf4.getText();

                tf5.setText(imie+" "+nazwisko+" "+stanowisko+" "+email);

            }

        });
        btnExit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        // Dodanie do okna
        frame.add(btnSubmit); frame.add(btnExit);
        frame.add(tf1); frame.add(tf2); frame.add(tf3);frame.add(tf4);frame.add(tf5);
        frame.add(lbl1); frame.add(lbl2); frame.add(lbl3); frame.add(lbl4);frame.add(lbl5);
        frame.add(lbl6);frame.add(lbl7);frame.add(lbl8);frame.add(lbl9);
        frame.add(checkbox1); frame.add(checkbox2); frame.add(checkbox3);
        frame.add(btn1); frame.add(btn2); frame.add(btn3);
        frame.getContentPane().add(kurs);
        frame.setSize(800, 600);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:0)

这是更正后的代码。

import java.awt.Checkbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class CzwartaApp {
    public static void main(String[] args) {
        // Klasy obiektow
        JFrame frame = new JFrame("Czwarta Aplikacja");

        JButton btnSubmit = new JButton("Wypisz");
        JButton btnExit = new JButton("Wyjdz");

        JLabel lbl1 = new JLabel("imie");
        JLabel lbl2 = new JLabel("nazwisko");
        JLabel lbl3 = new JLabel("stanowisko");
        JLabel lbl4 = new JLabel("E-mail");
        JLabel lbl5 = new JLabel("");
        JLabel lbl6 = new JLabel("Jakie znasz jezyki programowania?");
        JLabel lbl7 = new JLabel("Wybierz poziom jezyka angielskiego?");
        JLabel lbl8 = new JLabel("Wybierz kurs programowania?");
        JLabel lbl9 = new JLabel("Dane Kontaktowe");

        final JTextField tf1 = new JTextField();
        final JTextField tf2 = new JTextField();
        final JTextField tf3 = new JTextField();
        final JTextField tf4 = new JTextField();
        final JTextArea tf5 = new JTextArea();

        final Checkbox checkbox1 = new Checkbox("Java");
        final Checkbox checkbox2 = new Checkbox("Python");
        final Checkbox checkbox3 = new Checkbox("Inne");

        JRadioButton btn1 = new JRadioButton();
        btn1.setText("podstawowy");
        JRadioButton btn2 = new JRadioButton();
        btn2.setText("srednio-zaawansowany");
        JRadioButton btn3 = new JRadioButton();
        btn3.setText("zaawansowany");
        ButtonGroup group = new ButtonGroup();
        group.add(btn1);
        group.add(btn2);
        group.add(btn3);

        final JComboBox kurs = new JComboBox();
        kurs.addItem("FrontEnd Developer");
        kurs.addItem("BackEnd Developer");

        // Pozycjonowanie
        lbl1.setBounds(20, 20, 100, 20);
        tf1.setBounds(20, 70, 100, 20);
        lbl2.setBounds(20, 120, 100, 20);
        tf2.setBounds(20, 170, 100, 20);
        lbl3.setBounds(20, 220, 100, 20);
        tf3.setBounds(20, 270, 100, 20);
        lbl4.setBounds(20, 320, 100, 20);
        tf4.setBounds(20, 370, 100, 20);
        tf5.setBounds(500, 20, 250, 300);
        lbl5.setBounds(20, 420, 300, 20);
        lbl6.setBounds(200, 20, 250, 20);
        lbl7.setBounds(200, 220, 250, 20);
        lbl8.setBounds(200, 350, 250, 20);
        lbl9.setBounds(500, 50, 200, 20);
        btnSubmit.setBounds(20, 470, 100, 20);
        btnExit.setBounds(220, 470, 100, 20);
        checkbox1.setBounds(200, 70, 250, 20);
        checkbox2.setBounds(200, 120, 250, 20);
        checkbox3.setBounds(200, 170, 250, 20);
        btn1.setBounds(200, 250, 250, 20);
        btn2.setBounds(200, 280, 250, 20);
        btn3.setBounds(200, 310, 250, 20);
        kurs.setBounds(200, 380, 250, 20);

        // Obsluga zdarzen
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String imie = tf1.getText();
                String nazwisko = tf2.getText();
                String stanowisko = tf3.getText();
                String email = tf4.getText();

                StringBuffer buffer = new StringBuffer();
                buffer.append( "imie : ").append( imie ).append("\n");
                buffer.append( "nazwisko : ").append( nazwisko ).append("\n");
                buffer.append( "stanowisko : ").append( stanowisko ).append("\n");
                buffer.append( "E-mail : ").append( email ).append("\n");
                buffer.append( "Jakie znasz jezyki programowania? : ");
                if( checkbox1.getState() ) {
                    buffer.append( checkbox1.getLabel() ).append(",");
                }
                if( checkbox2.getState() ) {
                    buffer.append( checkbox2.getLabel() ).append(",");
                }
                if( checkbox3.getState() ) {
                    buffer.append( checkbox3.getLabel() );
                }
                buffer.append("\n");

                buffer.append( "Wybierz kurs programowania? ").append( kurs.getSelectedItem().toString() ).append("\n");



                tf5.setText(buffer.toString());

            }

        });
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        // Dodanie do okna
        frame.add(btnSubmit);
        frame.add(btnExit);
        frame.add(tf1);
        frame.add(tf2);
        frame.add(tf3);
        frame.add(tf4);
        frame.add(tf5);
        frame.add(lbl1);
        frame.add(lbl2);
        frame.add(lbl3);
        frame.add(lbl4);
        frame.add(lbl5);
        frame.add(lbl6);
        frame.add(lbl7);
        frame.add(lbl8);
        frame.add(lbl9);
        frame.add(checkbox1);
        frame.add(checkbox2);
        frame.add(checkbox3);
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.getContentPane().add(kurs);
        frame.setSize(800, 600);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}