我试图使此代码正常工作,以便我有一个cardlayout容器,并且每个面板都在其自己的类和实际文件中定义。这段代码不是我自己的100%,而是另一个Stack溢出用户对我以前的东西的修改版本。它或多或少是我所需要的,但是我需要它使得它不是自动化的,我可以编写15个不同的面板,并在每个面板中做出决策。所述用户修改了Main和Arrow类,而Imagepanel是我尝试编写一个将被代码工作部分接受的类。问题是我插入到容器中的Imagepanel将注册为现有容器,但面板上没有任何显示,为空白。 ImagePanel中注释掉的部分是我在后台刻录机上设置的代码,以支持先前在Arrow类中使用的已建立内容。

这是主班

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;

public class Main extends JPanel {
   private Arrow arrow = new Arrow();   //creates a new Arrow object

public Main() {
    JPanel btnPanel = new JPanel();
    btnPanel.add(new JButton(new NextAction("Next")));

    setLayout(new BorderLayout());
    add(arrow, BorderLayout.NORTH);
    add(btnPanel, BorderLayout.PAGE_END);
}

private class NextAction extends AbstractAction {
    public NextAction(String name) {
        super(name);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        arrow.next();  // *** call arrow's public next method that you created

        // no need to make a new CardLayout instance
    }
}

private static void createAndShowGui() {
    Main mainPanel = new Main();

    JFrame frame = new JFrame("Iowa Budget Simulation");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> createAndShowGui());
}


}

这是创建容器的Arrow类

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.*;

class Arrow extends JPanel {
    private static final long serialVersionUID = 1L;
    private CardLayout cardLayout = new CardLayout();       // make me a field
    private JPanel cardHolder = new JPanel(cardLayout);     //creates a master JPanel

public Arrow() {
    for (int i = 0; i < 5; i++) {
        cardHolder.add(createCard(i), "card " + i);
    }
    ImagePanel pear = new ImagePanel();
    cardHolder.add(pear, "Pear");
    setLayout(new BorderLayout());
    add(cardHolder, BorderLayout.NORTH);

}

// public method that other objects can call
public void next() {
    cardLayout.next(cardHolder);  // call next on the correct object
}

// simply creates a "pretty" new JPanel
private JComponent createCard(int i) {
    JLabel label = new JLabel("Card " + i);
    label.setFont(label.getFont().deriveFont(Font.BOLD, 50f));

    float h = (float)Math.random();
    Color c = Color.getHSBColor(h, 1f, 1f);
    label.setForeground(c.darker());

    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(label);
    panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
    panel.setBackground(c.brighter().brighter());

    panel.setPreferredSize(new Dimension(400, 300));
    return panel;
}


这是ImagePanel,我尝试的是第三个单独的面板/类

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.*;

class ImagePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private String imgString;
    private JLabel imgLabel;

public JComponent ImagePanel() {
    /*
    setName("Pear");
    JLabel john = new JLabel("Pear");

    float h = (float)Math.random();
    Color c = Color.getHSBColor(h, 1f, 1f);
    john.setForeground(c.darker());

    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(john);
    panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
    panel.setBackground(c.brighter().brighter());
    // Ensure size is correct even before any image is loaded.
    setPreferredSize(new Dimension(400, 300));
    return panel;
    */

    JLabel label = new JLabel("Pear");
    label.setFont(label.getFont().deriveFont(Font.BOLD, 50f));

    float h = (float)Math.random();
    Color c = Color.getHSBColor(h, 1f, 1f);
    label.setForeground(c.darker());

    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(label);
    panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
    panel.setBackground(c.brighter().brighter());

    panel.setPreferredSize(new Dimension(400, 300));
    return panel;
}


发布没有错误,它仅显示一个空白面板。感谢您提供的任何帮助,在通过YouTube学习Java Swing GUI和堆栈溢出时,我向您致歉。

最佳答案

public JComponent ImagePanel() {不是构造函数,它是一种方法,要使其在您的代码中起作用,您必须将ImagePanel pear = new ImagePanel();更改为JComponent pear = new ImagePanel().ImagePanel();,但坦率地说,这没有多大意义。

相反,将public JComponent ImagePanel() {更改为public ImagePanel() {,现在它是类的构造函数

接下来,改变...

JPanel panel = new JPanel(new GridBagLayout());
panel.add(label);
panel.setBorder(BorderFactory.createLineBorder(c.darker(), 20));
panel.setBackground(c.brighter().brighter());

panel.setPreferredSize(new Dimension(400, 300));
return panel;




setLayout(new GridBagLayout());
add(label);
setBorder(BorderFactory.createLineBorder(c.darker(), 20));
setBackground(c.brighter().brighter());

//panel.setPreferredSize(new Dimension(400, 300));
//return panel;


不要让我开始理解为什么setPreferredSize是个坏主意

现在您可以简单地使用

ImagePanel pear = new ImagePanel();
cardHolder.add(pear, "Pear");

10-07 16:50