Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在netbeans(JavaSwing)中查看另一个包含来自同一JFrame的许多子jPanel的jPanel_Java_Swing - Fatal编程技术网

如何在netbeans(JavaSwing)中查看另一个包含来自同一JFrame的许多子jPanel的jPanel

如何在netbeans(JavaSwing)中查看另一个包含来自同一JFrame的许多子jPanel的jPanel,java,swing,Java,Swing,我想显示按钮事件操作中的另一个jPanel。 e、 g 当我使用CardLayout时,我一次只能使用一个面板,难道没有办法在一个帧中添加多个面板,然后在事件切换到同一帧中的另一组多个面板吗 确切地说,每次使用CardLayout只能显示一个JPanel,但这并不妨碍您在使用它时显示多个JPanels 您需要制作卡(当前视图中显示的JPanel)以显示多个JPanel 例如: import java.awt.CardLayout; import java.awt.Color; import ja

我想显示按钮事件操作中的另一个jPanel。 e、 g


当我使用
CardLayout
时,我一次只能使用一个面板,难道没有办法在一个帧中添加多个面板,然后在事件切换到同一帧中的另一组多个面板吗

确切地说,每次使用
CardLayout
只能显示一个
JPanel
,但这并不妨碍您在使用它时显示多个
JPanel
s

您需要制作
卡(当前视图中显示的
JPanel
)以显示多个
JPanel

例如:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CardLayoutWithMultiplePanes {
    private JFrame frame;
    private JPanel pane;
    private JPanel cardsPane;
    private JPanel[] cards;
    private CardLayout cl;
    private JButton nextButton;
    private JButton previousButton;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CardLayoutWithMultiplePanes()::createAndShowGui);
    }
    
    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());
        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
        
        previousButton = new JButton("Previous");
        nextButton = new JButton("Next");
        
        cl = new CardLayout();
        cardsPane = new JPanel(cl);
        cards = new JPanel[2];
        
        for (int i = 0; i < cards.length; i++) {
            cards[i] = new JPanel();
            cards[i].setLayout(new GridLayout(2, 1));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.BLUE : Color.RED));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.GREEN : Color.MAGENTA));
            
            cardsPane.add(cards[i]);
        }
        
        Box box = Box.createHorizontalBox();
        box.add(previousButton);
        box.add(Box.createHorizontalGlue());
        box.add(nextButton);
        
        previousButton.addActionListener(listener);
        nextButton.addActionListener(listener);
        
        pane.add(cardsPane);
        pane.add(box);
        
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private ActionListener listener = e -> {
        if (e.getSource().equals(previousButton)) {
            cl.previous(cardsPane);
        } else if (e.getSource().equals(nextButton)) {
            cl.next(cardsPane);
        }
    };
    
    @SuppressWarnings("serial")
    class CustomPane extends JPanel {
        private Color color;
        
        public CustomPane(Color color) {
            this.color = color;
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    }
}
导入java.awt.CardLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.GridLayout;
导入java.awt.event.ActionListener;
导入javax.swing.Box;
导入javax.swing.BoxLayout;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
带有多页的公共类卡片布局{
私有JFrame;
私人JPanel窗格;
私人JPanel cardsPane;
私人JPanel[]卡;
私人信用卡;
私有JButton nextButton;
私有JButton-previousButton;
公共静态void main(字符串[]args){
InvokeLayoutWithMultiplePanes():createAndShowGui);
}
私有void createAndShowGui(){
frame=newjframe(getClass().getSimpleName());
pane=newjpanel();
pane.setLayout(新的BoxLayout(pane,BoxLayout.PAGE_轴));
previousButton=新的JButton(“先前”);
下一个按钮=新的按钮(“下一个”);
cl=新的CardLayout();
cardsPane=新JPanel(cl);
卡片=新JPanel[2];
对于(int i=0;i{
if(例如getSource().equals(previousButton)){
cl.先前(cardsPane);
}else if(例如getSource().equals(nextButton)){
cl.next(cardsPane);
}
};
@抑制警告(“串行”)
类CustomPane扩展了JPanel{
私人色彩;
公共自定义窗格(彩色){
这个颜色=颜色;
}
@凌驾
受保护组件(图形g){
超级组件(g);
g、 设置颜色(颜色);
g、 fillRect(0,0,getWidth(),getHeight());
}
@凌驾
公共维度getPreferredSize(){
返回新维度(100100);
}
}
}

上面的代码显示了一个
JPanel
,其中包含另外两个
JPanel
s,其中每个
JPanel
都有自己的背景色(并且可能包含自己的组件,如
JLabel
JButton
等)

我希望这能让你知道你想做什么

注:
  • 把你的
    JFrame
    想象成一个笔记本
  • JPanel
    s想象为图纸
  • 想象一下
    CardLayout
    当你的手指(前后)传递页面时

在每一张纸(
JPanel
)中,你可以拥有你想要的任何东西(甚至不止一张纸(粘在上面)),这里的原则是相同的

使用a,看到这个给你一个想法,我可以在
绝对布局
中做什么@不要用它<代码>绝对布局
=
空布局
=和。。。下面是一个例子,当你使用它们时会发生什么…好的,谢谢你的帮助!当我使用
CardLayout
时,我一次只能使用一个面板,难道没有办法在一个帧中添加多个面板,然后在事件切换到同一帧中的另一组多个面板吗@Frakcool
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CardLayoutWithMultiplePanes {
    private JFrame frame;
    private JPanel pane;
    private JPanel cardsPane;
    private JPanel[] cards;
    private CardLayout cl;
    private JButton nextButton;
    private JButton previousButton;
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CardLayoutWithMultiplePanes()::createAndShowGui);
    }
    
    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());
        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
        
        previousButton = new JButton("Previous");
        nextButton = new JButton("Next");
        
        cl = new CardLayout();
        cardsPane = new JPanel(cl);
        cards = new JPanel[2];
        
        for (int i = 0; i < cards.length; i++) {
            cards[i] = new JPanel();
            cards[i].setLayout(new GridLayout(2, 1));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.BLUE : Color.RED));
            cards[i].add(new CustomPane((i + 1) % 2 == 0 ? Color.GREEN : Color.MAGENTA));
            
            cardsPane.add(cards[i]);
        }
        
        Box box = Box.createHorizontalBox();
        box.add(previousButton);
        box.add(Box.createHorizontalGlue());
        box.add(nextButton);
        
        previousButton.addActionListener(listener);
        nextButton.addActionListener(listener);
        
        pane.add(cardsPane);
        pane.add(box);
        
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private ActionListener listener = e -> {
        if (e.getSource().equals(previousButton)) {
            cl.previous(cardsPane);
        } else if (e.getSource().equals(nextButton)) {
            cl.next(cardsPane);
        }
    };
    
    @SuppressWarnings("serial")
    class CustomPane extends JPanel {
        private Color color;
        
        public CustomPane(Color color) {
            this.color = color;
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(100, 100);
        }
    }
}