繁体   English   中英

为什么JButton不显示在JFrame中?

[英]Why are JButtons not displaying in a JFrame?

我正在尝试开发一个投票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
        }
    }
}

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

有关更多详细信息,请参见如何使用BorderLayout在容器中布置组件

例如...

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
            }
        }
    }
}

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

使用不同的位置或布局

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM