JButton + radiobox +复选框

时间:2014-01-24 19:31:27

标签: java swing jtable jbutton

我想要这个程序我必须有一些“总和”按钮,它会在“描述”栏中添加有关电影的摘要信息。让我们说我已经“死硬”作为标题,7岁时从radiobutton,并从复选框中选择恐怖。按下按钮会在列下面显示“Die hard,7,horror”。我不知道如何处理这个案子。

 package naplety.Swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;

public class SamodzielnaListaOsob extends JFrame implements ActionListener {

    JButton dodaj, erease;
    JTextField film;
    DefaultListModel<String> listFilm;
    DefaultTableModel tableFilm;

    public SamodzielnaListaOsob(String title) {
        super(title);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        final JTextField film = new JTextField("Wpisz tytul filmu", 10);
        film.setBorder(BorderFactory.createTitledBorder("Film"));

        JPanel p1 = new JPanel();
        p1.add(film);

        JButton dodaj = new JButton("Add to list");
        dodaj.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nowyFilm = film.getText();
                if (nowyFilm != "") {
                    listFilm.addElement(nowyFilm);
                    film.setText("");
                }
            }
        });
        JButton erease = new JButton("Clear");
        erease.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                film.setText("");

            }
        });
        JButton dodajDoTabeli = new JButton("Add to table");
        dodajDoTabeli.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nowyFilm = film.getText();
                if (nowyFilm != "") {
                    int ile = tableFilm.getRowCount();
                    tableFilm.addRow(new Object[] { ile + 1, nowyFilm });
                }
            }
        });

        JRadioButton sevenbutton = new JRadioButton("7");
        JRadioButton twbutton = new JRadioButton("12");
        JRadioButton sixbutton = new JRadioButton("16");
        JRadioButton eightbutton = new JRadioButton("18");

        ButtonGroup bg1 = new ButtonGroup();
        bg1.add(sevenbutton);
        bg1.add(twbutton);
        bg1.add(sixbutton);
        bg1.add(eightbutton);

        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new GridLayout(4, 0));
        radioPanel.add(sevenbutton);
        radioPanel.add(twbutton);
        radioPanel.add(sixbutton);
        radioPanel.add(eightbutton);

        radioPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createEtchedBorder(), "Age"));
        radioPanel.setSize(200, 200);

        JCheckBox Horror = new JCheckBox("Horror");
        JCheckBox Komedia = new JCheckBox("Comedy");
        JCheckBox Thriller = new JCheckBox("Thriller");

        JCheckBoxMenuItem listac = new JCheckBoxMenuItem();
        listac.add(Horror);
        listac.add(Komedia);
        listac.add(Thriller);

        JPanel listaChceck = new JPanel();
        listaChceck.add(Horror);
        listaChceck.add(Komedia);
        listaChceck.add(Thriller);
        listaChceck.setLayout(new GridLayout(3, 0));

        JPanel p2 = new JPanel();
        p2.add(dodaj);
        p2.add(erease);
        p2.add(dodajDoTabeli);
        p2.add(radioPanel);
        p2.add(listaChceck);

        listFilm = new DefaultListModel<String>();
        listFilm.addElement("Achacy");
        listFilm.addElement("Bonifacy");
        listFilm.addElement("Cezary");

        JList<String> lista = new JList<String>(listFilm);
        JScrollPane sl = new JScrollPane(lista);
        sl.setPreferredSize(new Dimension(150, 150));
        sl.setBorder(BorderFactory.createTitledBorder("List"));

        String[] kolumnyTabeli = { "Nr", "Movie", "Description" };
        tableFilm = new DefaultTableModel(kolumnyTabeli, 0) {

        };
        JTable tabela = new JTable(tableFilm);
        JScrollPane st = new JScrollPane(tabela);
        st.setPreferredSize(new Dimension(300, 150));
        st.setBorder(BorderFactory.createTitledBorder("Table"));

        JPanel p3 = new JPanel();
        p3.add(sl);
        p3.add(st);

        setPreferredSize(new Dimension(900, 900));
        setVisible(true);
        p1.add(p2);
        p2.add(p3);
        setContentPane(p1);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SamodzielnaListaOsob("List of movies");
            }
        });
    }

}

2 个答案:

答案 0 :(得分:1)

  1. 您需要在尝试访问变量之前声明变量,或者将变量声明为全局变量。我更喜欢这种方式。

  2. 在你的框架上使用.pack()来启动程序,实际显示的内容。

  3. 学习使用LayoutManagers以获得更好的外观。

  4. 使用RadioButtonsCheckBoxes的数组,以便更容易遍历它们。我必须手动编写一堆if语句,如果我可以遍历它们就没有必要。

  5. 要获取是选择了RadioButton或CheckBox,请使用.isSelected()

  6. .setVisible(true) 之后添加所有组件。


  7. 这是您重构的代码。我没有做任何其他事情,但修复了问题中发布的问题。现在,当您点击Add Film按钮时,它会添加信息desciption。

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class SamodzielnaListaOsob extends JFrame {
    
        JButton dodaj, erease;
        JTextField film;
        DefaultListModel<String> listFilm;
        DefaultTableModel tableFilm;
        JList<String> lista = null;
    
        JRadioButton sevenbutton = new JRadioButton("7");
        JRadioButton twbutton = new JRadioButton("12");
        JRadioButton sixbutton = new JRadioButton("16");
        JRadioButton eightbutton = new JRadioButton("18");
    
        JCheckBox Horror = new JCheckBox("Horror");
        JCheckBox Komedia = new JCheckBox("Comedy");
        JCheckBox Thriller = new JCheckBox("Thriller");
    
        ButtonGroup bg1 = new ButtonGroup();
    
        public SamodzielnaListaOsob(String title) {
            super(title);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            final JTextField film = new JTextField("Wpisz tytul filmu", 10);
            film.setBorder(BorderFactory.createTitledBorder("Film"));
    
            JPanel p1 = new JPanel();
            p1.add(film);
    
            JButton dodaj = new JButton("Add to list");
            dodaj.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String nowyFilm = film.getText();
                    if (nowyFilm != "") {
                        listFilm.addElement(nowyFilm);
                        film.setText("");
                    }
                }
            });
            JButton erease = new JButton("Clear");
            erease.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    film.setText("");
    
                }
            });
            JButton dodajDoTabeli = new JButton("Add to table");
            dodajDoTabeli.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String nowyFilm = film.getText();
                    if (nowyFilm != "") {
                        int ile = tableFilm.getRowCount();
                        String title = lista.getSelectedValue();
                        int age;
                        if (sixbutton.isSelected()) {
                            age = 16;
                        } else if (sevenbutton.isSelected()) {
                            age = 7;
                        } else if (eightbutton.isSelected()) {
                            age = 18;
                        } else {
                            age = 12;
                        }
                        String genres = "";
                        if (Horror.isSelected()) {
                            genres += "Horror, ";
                        }
                        if (Komedia.isSelected()) {
                            genres += "Komedia, ";
                        }
                        if (Thriller.isSelected()) {
                            genres += "Thriller";
                        }
    
                        String desc = title + ", " + age + ", " + genres;
                        tableFilm.addRow(new Object[]{ile + 1, nowyFilm, desc});
                    }
                }
            });
    
            bg1.add(sevenbutton);
            bg1.add(twbutton);
            bg1.add(sixbutton);
            bg1.add(eightbutton);
    
            JPanel radioPanel = new JPanel();
            radioPanel.setLayout(new GridLayout(4, 0));
            radioPanel.add(sevenbutton);
            radioPanel.add(twbutton);
            radioPanel.add(sixbutton);
            radioPanel.add(eightbutton);
    
            radioPanel.setBorder(BorderFactory.createTitledBorder(
                    BorderFactory.createEtchedBorder(), "Age"));
            radioPanel.setSize(200, 200);
    
            JCheckBoxMenuItem listac = new JCheckBoxMenuItem();
            listac.add(Horror);
            listac.add(Komedia);
            listac.add(Thriller);
    
            JPanel listaChceck = new JPanel();
            listaChceck.add(Horror);
            listaChceck.add(Komedia);
            listaChceck.add(Thriller);
            listaChceck.setLayout(new GridLayout(3, 0));
    
            JPanel p2 = new JPanel();
            p2.add(dodaj);
            p2.add(erease);
            p2.add(dodajDoTabeli);
            p2.add(radioPanel);
            p2.add(listaChceck);
    
            listFilm = new DefaultListModel<String>();
            listFilm.addElement("Achacy");
            listFilm.addElement("Bonifacy");
            listFilm.addElement("Cezary");
    
            lista = new JList<String>(listFilm);
            JScrollPane sl = new JScrollPane(lista);
            sl.setPreferredSize(new Dimension(150, 150));
            sl.setBorder(BorderFactory.createTitledBorder("List"));
    
            String[] kolumnyTabeli = {"Nr", "Movie", "Description"};
            tableFilm = new DefaultTableModel(kolumnyTabeli, 0) {
    
            };
            JTable tabela = new JTable(tableFilm);
            JScrollPane st = new JScrollPane(tabela);
            st.setPreferredSize(new Dimension(300, 150));
            st.setBorder(BorderFactory.createTitledBorder("Table"));
    
            JPanel p3 = new JPanel();
            p3.add(sl);
            p3.add(st);
    
    
            p1.add(p2);
            p2.add(p3);
            setContentPane(p1);
    
            pack();
            setPreferredSize(new Dimension(900, 900));
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new SamodzielnaListaOsob("List of movies");
                }
            });
        }
    
    }
    

答案 1 :(得分:0)

单选按钮和检查按钮不像文本字段那样工作。在文本字段中,有用的值是从“text”成员中获取的(例如:getText())。而radioButton则定义了一组固定的选择(好吧,你可以让它们变得动态,但它不值得,有更好的组件用于那种工作),如“是”和“否”。通常,当你拾取一些rabiobutton时,你使用isChecked()方法(返回boolean,它可能是isChecked(),我不记得)以这种或那种方式作出反应。例如:

String age = 0;
if(sevenbutton.isSelected()){
    age = 7;
}

尽管如此,我认为您可以使用getText()从七个按钮中的文本中获取值,但是您还需要检查哪个radiobutton已经被检查。 checkButton以非常类似的方式工作,但对于非独占选择,所以无论如何你需要使用isSelected()方法或类似方法。 您的方法应如下所示:

private void addMovie(){
   String age = "0";
   String gender = "";
   //Evaluate radioButtons
   if(sevenbutton.isSelected()){
         age = 7;
   }
   //Evaluate checkbuttons
   if(Horror.isSelected()){
        gender = gender+" horror";
   }

   String movie = film+" "+age+" "+gender;
   //do anything else
}