为什么JButton没有显示在JFrame中?

时间:2015-11-26 22:49:22

标签: java swing oop

我正在尝试开发一个投票GUI,我有一个主类和一个选票类。 Ballot类扩展了JPanel并在类中创建了按钮。我试图将Ballot对象添加到主JFrame,但是当我运行程序时,按钮不会显示。任何帮助,将不胜感激。这是代码。

Assig5.java:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.BoxLayout;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;



public class Assig5 extends JFrame
{
    public Assig5()
    {
        super("E-Vote Version 1.0");
        JPanel castVotePanel = new JPanel();
        BoxLayout layout = new BoxLayout(castVotePanel, BoxLayout.Y_AXIS);
        castVotePanel.setLayout(layout);
        ArrayList<String> ballots = new ArrayList<String>();

        try{
            ballots = readBallotFile("ballots.txt");
        }
        catch(FileNotFoundException e){
            System.exit(0);
        }

        ArrayList<Ballot> ballotList = addBallots(ballots);
        for(Ballot b : ballotList)
        {
            add(b);
        }

        castVotePanel.add(createLoginButton());
        castVotePanel.add(createCastButton());

        add(castVotePanel);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String args[])
    {
        Assig5 assig5 = new Assig5();
    }

    public JButton createLoginButton()
    {
        JButton loginButton = new JButton("Login to Vote");
        loginButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                // display/center the jdialog when the button is pressed
                String input = JOptionPane.showInputDialog("Please enter your voter ID: ");
            }
        });
        return loginButton;
    }



    public JButton createCastButton()
    {
        JButton castButton = new JButton("Cast Vote");
        castButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {

            }
        });
        return castButton;
    }

    public ArrayList<Ballot> addBallots(ArrayList<String> ballotContents)
    {
        ArrayList<Ballot> ballotList = new ArrayList<Ballot>();
        for(int i = 0; i < ballotContents.size(); i++)
        {

            String[] splitBallotContent = ballotContents.get(i).split("[:,]");
            String[] options = new String[splitBallotContent.length - 2];
            for(int j = 2; j < splitBallotContent.length; j++)
            {
                options[j - 2] = splitBallotContent[j];
            }
            Ballot ballot = new Ballot(splitBallotContent[0], splitBallotContent[1], options);
            ballotList.add(ballot);
        }
        return ballotList;
    }

    public static ArrayList<String> readBallotFile(String filename) throws FileNotFoundException
    {
        ArrayList<String> list = new ArrayList<String>();
        Scanner s = new Scanner(new File(filename));
        int numBallots = Integer.parseInt(s.nextLine()); //we need to get to next line
        for(int i = 0; i < numBallots; i++)
        {
            if(s.hasNextLine())
            {
                list.add(s.nextLine());
            }

        }
        s.close();
        return list;
    }

Ballot.java:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.BoxLayout;
import java.awt.*;
import java.awt.event.*;

public class Ballot extends JPanel
{
    public Ballot(String ballotID, String title, String[] options)
    {
        super();
        BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
        setLayout(layout);
        add(new JLabel(title, JLabel.CENTER));
        for(String s : options)
        {
            add(new JButton(s));
            //add actionlistener here
        }
    }
}

2 个答案:

答案 0 :(得分:1)

JFrame使用BorderLayout,您要将castVotePanel和所有Ballot面板添加到框架(CENTER)上的相同位置。您可能需要考虑使用其他布局管理器

有关详细信息,请参阅How to Use BorderLayoutLaying Out Components Within a Container

例如......

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Assig5 extends JFrame {

    public Assig5() {
        super("E-Vote Version 1.0");
        JPanel castVotePanel = new JPanel();
        BoxLayout layout = new BoxLayout(castVotePanel, BoxLayout.Y_AXIS);
        castVotePanel.setLayout(layout);
        ArrayList<String> ballots = new ArrayList<String>();

        try {
            ballots = readBallotFile("ballots.txt");
        } catch (FileNotFoundException e) {
            System.exit(0);
        }

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;

        ArrayList<Ballot> ballotList = addBallots(ballots);
        for (Ballot b : ballotList) {
            add(b, gbc);
        }

        castVotePanel.add(createLoginButton());
        castVotePanel.add(createCastButton());

        add(castVotePanel, gbc);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        Assig5 assig5 = new Assig5();
    }

    public JButton createLoginButton() {
        JButton loginButton = new JButton("Login to Vote");
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // display/center the jdialog when the button is pressed
                String input = JOptionPane.showInputDialog("Please enter your voter ID: ");
            }
        });
        return loginButton;
    }

    public JButton createCastButton() {
        JButton castButton = new JButton("Cast Vote");
        castButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });
        return castButton;
    }

    public ArrayList<Ballot> addBallots(ArrayList<String> ballotContents) {
        ArrayList<Ballot> ballotList = new ArrayList<Ballot>();
        int id = 0;
        for (int i = 0; i < 10; i++) {

            String[] options = new String[]{"A", "B", "C", "D"};
            Ballot ballot = new Ballot(Integer.toString(++id), "Help " + id, options);
            ballotList.add(ballot);
        }
        return ballotList;
    }

    public static ArrayList<String> readBallotFile(String filename) throws FileNotFoundException {
        ArrayList<String> list = new ArrayList<String>();
//        Scanner s = new Scanner(new File(filename));
//        int numBallots = Integer.parseInt(s.nextLine()); //we need to get to next line
//        for (int i = 0; i < numBallots; i++) {
//            if (s.hasNextLine()) {
//                list.add(s.nextLine());
//            }
//
//        }
//        s.close();
        return list;

    }

    public class Ballot extends JPanel {

        public Ballot(String ballotID, String title, String[] options) {
            super();
            BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
            setLayout(layout);
            add(new JLabel(title, JLabel.CENTER));
            for (String s : options) {
                add(new JButton(s));
                //add actionlistener here
            }
        }
    }
}

答案 1 :(得分:0)

默认情况下,JFrame使用BorderLayout,并且所有面板都位于边框的中心,这就是为什么它不显示

使用不同的位置或不同的布局

了解有关布局的更多信息:https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html