Java GUI没有显示任何内容

时间:2016-11-20 17:51:49

标签: java user-interface

我是Java的新手,我正在创建一个Gui。我没有任何错误,但是当我跑步时,没有显示任何内容。 我不明白为什么。可以请别人帮忙吗?

这是我的GUI代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

public class DartsGUI implements ActionListener {

    //Gui components
    JFrame mainDartsFrame;
    JPanel buttonsPanel,infoPanel;
    JRadioButton viewTableAsc,viewTableDesc,editScores,viewDetails,searchById;
    CardLayout cLayout;
    JTextArea table1,table2;
    JScrollPane scrollTable1,scrollTable2;
    JLabel edit,details,search;

    public void DartGUI() {
        mainDartsFrame= new JFrame("Darts Competition");
        mainDartsFrame.setSize(700, 500);
        mainDartsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainDartsFrame.setLayout(new BorderLayout());
        mainDartsFrame.setLocationRelativeTo(null);
        createButtonsPanel();
        createInfoPanel();
        mainDartsFrame.setVisible(true);
    }

    private void createButtonsPanel() {
        JPanel buttonsPanel = new JPanel( new GridLayout(5,1) );
        viewTableAsc= new JRadioButton("Info Ascending Order");
        viewTableAsc.addActionListener(this);
        viewTableDesc= new JRadioButton("Info Descending Order");
        viewTableDesc.addActionListener(this);
        editScores= new JRadioButton("Edit Scores");
        editScores.addActionListener(this);
        viewDetails= new JRadioButton("View Details");
        viewDetails.addActionListener(this);
        searchById=new JRadioButton("Search Competitor");
        searchById.addActionListener(this);

        //Group radio buttons to ensure only one is chosen each time
        ButtonGroup radioButtons = new ButtonGroup();
        radioButtons.add(searchById);
        radioButtons.add(viewTableAsc);
        radioButtons.add(viewTableDesc);
        radioButtons.add(editScores);
        radioButtons.add(viewDetails);
        //Push buttons into the buttons Panel
        buttonsPanel.add(viewTableAsc);                    
        buttonsPanel.add(viewTableDesc);                            
        buttonsPanel.add(editScores);                        
        buttonsPanel.add(viewDetails);                            
        buttonsPanel.add(searchById);                            

        mainDartsFrame.add(buttonsPanel,BorderLayout.WEST);
    }

    private void createInfoPanel() {
        infoPanel = new JPanel();
        cLayout = new CardLayout();
        infoPanel.setLayout(cLayout);
        table1 = new JTextArea(10,10);
        table1.setEditable(false);
        scrollTable1 = new JScrollPane(table1);
        table2 = new JTextArea(10,10);
        table2.setEditable(false);
        scrollTable2 = new JScrollPane(table1);
        edit = new JLabel("Edit Scores");
        search= new JLabel("Search Id");
        details=new JLabel("View Details");

        infoPanel.add(scrollTable1, "viewTable1");
        infoPanel.add(scrollTable2, "viewTable2");
        infoPanel.add(edit, "edit");
        infoPanel.add(search, "searchId");
        infoPanel.add(details, "viewDetails");

        mainDartsFrame.add(infoPanel,BorderLayout.CENTER);
    }

    public void actionPerformed (ActionEvent e) {
        JRadioButton eventButton = (JRadioButton) e.getSource();

        if(eventButton==viewTableAsc){
            cLayout.show(infoPanel,"viewTable1");
        }
        else if(eventButton==viewTableDesc){
            cLayout.show(infoPanel, "viewTable2");
        }
        else if(eventButton==editScores){
            cLayout.show(infoPanel, "edit");
        }
        else if(eventButton==viewDetails){
            cLayout.show(infoPanel, "viewDetails");
        }
        else{
            cLayout.show(infoPanel, "searchId");
        }
    }
}

主要课程:

public class mainGui {

    public static void main(String[] args) {
        DartsGUI gui = new DartsGUI();
    }
}

2 个答案:

答案 0 :(得分:3)

您需要调用DartGUI方法:

public static void main(String[] args) {
    DartsGUI gui = new DartsGUI();
    gui.DartGUI();
}

答案 1 :(得分:1)

您正在使用方法而不是构造函数。

以下是您的代码应该如何:

public class DartsGUI implements ActionListener {
//Gui components
JFrame mainDartsFrame;
JPanel buttonsPanel,infoPanel;
JRadioButton viewTableAsc,viewTableDesc,editScores,viewDetails,searchById;
CardLayout cLayout;
JTextArea table1,table2;
JScrollPane scrollTable1,scrollTable2;
JLabel edit,details,search;

public DartGUI(){
    mainDartsFrame= new JFrame("Darts Competition");
    mainDartsFrame.setSize(700, 500);
    mainDartsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainDartsFrame.setLayout(new BorderLayout());
    mainDartsFrame.setLocationRelativeTo(null);
    createButtonsPanel();
    createInfoPanel();
    mainDartsFrame.setVisible(true);
}

private void createButtonsPanel(){

    JPanel buttonsPanel = new JPanel( new GridLayout(5,1) );
    viewTableAsc= new JRadioButton("Info Ascending Order");
    viewTableAsc.addActionListener(this);
    viewTableDesc= new JRadioButton("Info Descending Order");
    viewTableDesc.addActionListener(this);
    editScores= new JRadioButton("Edit Scores");
    editScores.addActionListener(this);
    viewDetails= new JRadioButton("View Details");
    viewDetails.addActionListener(this);
    searchById=new JRadioButton("Search Competitor");
    searchById.addActionListener(this);

    //Group radio buttons to ensure only one is chosen each time
    ButtonGroup radioButtons = new ButtonGroup();
    radioButtons.add(searchById);
    radioButtons.add(viewTableAsc);
    radioButtons.add(viewTableDesc);
    radioButtons.add(editScores);
    radioButtons.add(viewDetails);
    //Push buttons into the buttons Panel
    buttonsPanel.add(viewTableAsc);                 
    buttonsPanel.add(viewTableDesc);                            
    buttonsPanel.add(editScores);                       
    buttonsPanel.add(viewDetails);                          
    buttonsPanel.add(searchById);                           

    mainDartsFrame.add(buttonsPanel,BorderLayout.WEST);
}

private void createInfoPanel(){
    infoPanel = new JPanel();
    cLayout = new CardLayout();
    infoPanel.setLayout(cLayout);
    table1 = new JTextArea(10,10);
    table1.setEditable(false);
    scrollTable1 = new JScrollPane(table1);
    table2 = new JTextArea(10,10);
    table2.setEditable(false);
    scrollTable2 = new JScrollPane(table1);
    edit = new JLabel("Edit Scores");
    search= new JLabel("Search Id");
    details=new JLabel("View Details");

    infoPanel.add(scrollTable1, "viewTable1");
    infoPanel.add(scrollTable2, "viewTable2");
    infoPanel.add(edit, "edit");
    infoPanel.add(search, "searchId");
    infoPanel.add(details, "viewDetails");

    mainDartsFrame.add(infoPanel,BorderLayout.CENTER);
}
    public void actionPerformed (ActionEvent e){
        JRadioButton eventButton = (JRadioButton) e.getSource();

        if(eventButton==viewTableAsc){
            cLayout.show(infoPanel,"viewTable1");
        }
        else if(eventButton==viewTableDesc){
            cLayout.show(infoPanel, "viewTable2");
        }
        else if(eventButton==editScores){
            cLayout.show(infoPanel, "edit");
        }
        else if(eventButton==viewDetails){
            cLayout.show(infoPanel, "viewDetails");
        }
        else{
            cLayout.show(infoPanel, "searchId");
        }

    }
}