我想開發一個投票GUI,我有一個主類和一個選票類。 Ballot類擴展JPanel並在類中創建按鈕。我試圖將Ballot對象添加到主JFrame,但是當我運行程序時,按鈕不顯示。任何幫助,將不勝感激。這是代碼。爲什麼JButton不能顯示在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
}
}
}
難道只有'JButton's不在顯示? –
'JFrame'使用'BorderLayout',您將'castVotePanel'和所有'Ballot'面板添加到框架上相同的位置('CENTER')。您可能想考慮使用不同的佈局管理器 – MadProgrammer