簡體   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