gpt4 book ai didi

java - 将 JPanel 设置为具有布局管理器时出现问题

转载 作者:行者123 更新时间:2023-12-02 04:18:52 25 4
gpt4 key购买 nike

我想现在已经很晚了,我的大脑停止了工作,我已经盯着这个看了好一个小时了,似乎无法弄清楚我的错误。简而言之,我的程序将显示所有州的投票区,并在单击每个州的按钮后,在其下方为每个政党(民主党,众议员,工业党)显示一个单选按钮,然后为每个州记录一个计数器,并在出现获胜者时返回获胜者政党达到 270 票。

我正在尝试使用 cardLayout 将状态名称和按钮显示为单个组件,然后我计划使用 GridLayout 在一帧上显示所有状态,但我有点超前了。

现在我的问题在于行 JPanel cards = new JPanel(new CardLayout()); ,错误告诉我构造函数未定义。我尝试用谷歌搜索该错误,但没有太多关于如何修复它的明确信息。

public class ElectoralCollegeGUI extends JFrame 
{
private static final long serialVersionUID = 1L;

public ElectoralCollegeGUI()
{
super("Cast Your Votes");
//JFrame frame = new JFrame();
//setLayout(new GridLayout(14, ))
JPanel mainPanel = new JPanel();
setLayout(new FlowLayout());
JPanel buttonPanel = new JPanel();
JPanel stateNames = new JPanel();

JRadioButton Democrat,Republican,Independent ;
ButtonGroup party;
party = new ButtonGroup();

Democrat = new JRadioButton("Democrat");
Republican = new JRadioButton("Republican");
Independent = new JRadioButton("Independent");

add(buttonPanel,BorderLayout.CENTER);

//adds buttons to party button group
party.add(Democrat);
party.add(Republican);
party.add(Independent);

//adds buttons to button panel
buttonPanel.add(Democrat);
buttonPanel.add(Republican);
buttonPanel.add(Independent);

class CardLayout implements ItemListener
{

public void addComponentToPane(Container pane)
{
JLabel[] state = {new JLabel("Alabama"), new JLabel("Alaska"), new JLabel("Arizona"), new JLabel("Arkansas"), new JLabel("California"),
new JLabel("Colorado"), new JLabel("Connecticut"), new JLabel("Delaware"), new JLabel("Florida"), new JLabel("Georgia"),
new JLabel("Hawaii"), new JLabel("Idaho"), new JLabel("Illinois"), new JLabel("Indiana"), new JLabel("Iowa"), new JLabel("Kansas"),
new JLabel("Kentucky"), new JLabel("Louisiana"), new JLabel("Maine 1st"), new JLabel("Maine 2nd"), new JLabel("Maine Popular"),
new JLabel("Maryland"), new JLabel("Massachusetts"), new JLabel("Michigan"), new JLabel("Minnesota"), new JLabel("Mississippi"),
new JLabel("Missouri"), new JLabel("Montant"), new JLabel("Nebraska 1st"), new JLabel("Nebraska 2nd"), new JLabel("Nebraska 3rd"),
new JLabel("Nebraska Popular"), new JLabel("Nevada"), new JLabel("New Hampshire"), new JLabel("New Jersey"), new JLabel("New Mexico"),
new JLabel("New York"), new JLabel("North Carolina"), new JLabel("North Dakota"), new JLabel("Ohio"), new JLabel("Oklahoma"),
new JLabel("Oregon"), new JLabel("Pennsylvania"), new JLabel("Rhode Island"), new JLabel("South Carolina"), new JLabel("South Dakota"),
new JLabel("Tennessee"), new JLabel("Texas"),new JLabel("Utah"),new JLabel("Vermont"),new JLabel("Virginia"),new JLabel("Washington"),
new JLabel("West Virginia"),new JLabel("Wisconsin"),new JLabel("Wyoming"),new JLabel("Washington,D.C."),};

for (int i = 0; i < state.length ; i++)
{
stateNames.add(state[i]); //stateNames is a JPanel already
}
for( int j = 0; j<state.length; j++)
{
stateNames.setText(stateNames.getText(state[j]));
}

//Create the "cards".
JPanel buttonsCard = new JPanel();
buttonsCard.add(new JRadioButton("Democrat"));
buttonsCard.add(new JRadioButton("Republican"));
buttonsCard.add(new JRadioButton("Indepdent"));

//JPanel card2 = new JPanel();


//Create the panel that contains the "cards".

JPanel cards = new JPanel(new CardLayout());

pane.add(stateNames, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}

public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());
}

最佳答案

您有一个自定义类 CardLayout,它在行 JPanel cards = new JPanel(new CardLayout()); 中使用,而不是标准类 >CardLayout 按预期。要么重命名您的类(这肯定是最简单且最不易混淆的),要么在需要时使用全名java.awt.CardLayout

关于java - 将 JPanel 设置为具有布局管理器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33008130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com