的JButton上的JPanel其所不应 [英] JButton are on JPanel on which it isn't should be

查看:240
本文介绍了的JButton上的JPanel其所不应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的具体问题。我试图给一个按钮,添加到一个面板循环。

Hi this is my concrete problem. I was tried to add one button to one panel with for loop.

这是循环创建Jbutton中。

This is for loop for creating JButtons.

nizButtona=new JButton[22];
for(int i=0;i<nizButtona.length;i++){

    nizButtona[i] = new JButton();
    if(i==0){
    nizButtona[i].setText("Započni kviz"); //Start quiz
    nizButtona[i].addActionListener(new ActionListener(){
         @Override 
        public void actionPerformed(ActionEvent e){
            cl.next(nizPanela[1]);
        }
    });
    }else if(i==1){
        nizButtona[i].setText("Izlaz"); //Quit
        nizButtona[i].addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                System.exit(0);
            }
        });
        }else if(i<12){
        nizButtona[i].setText("Sledeće pitanje"); //Next question, on next panel
        nizButtona[i].addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    cl.next(nizPanela[1]);
                }
            });
    }

这是面板上的添加按钮新的循环。这里nizButtona [I-1]为i-1,因为接下来的问题第一个按钮有超过JPanel的1论证它是必须添加的,你的GridBagLayout所有组件,所以我会把所有在每个面板的同一位置。没有它的问题是相同的。

This is new loop for adding buttons on panels. Here nizButtona[i-1] is i-1 because first button for next question have for 1 argument than JPanel where it's need to be add, and you GridBagLayout for all components so i will put all on the same location for each panel. Without it the problem is the same.

     for(int i=3;i<=11;i++){
        nizPanela[i].add(nizButtona[i-1]);
    }

下面是我是如何创建JPanels阵列。

Here is how i was create array for JPanels.

nizPanela =新JPanel [13];

nizPanela = new JPanel[13];

    for (int i=0;i<nizPanela.length;i++ ){

        nizPanela[i] = new JPanel();

        if(i<=1){
        okvir.getContentPane().add(nizPanela[i]);//Does i real need this getContentPane?
        }else{
            nizPanela[i].setLayout(new GridBagLayout());
            nizPanela[1].add(nizPanela[i], String.valueOf(i));
        }

    }
    cl=new CardLayout();

    nizPanela[1].setLayout(cl); 

    cl.show(nizPanela[1],"2");

这是怎么看的程序照片按钮Sledećepitanje此面板上可见,但它不应该的。如果我通过地方这个按钮移动鼠标指针,这只是可见。

This is how program look photo Button Sledeće pitanje visible on this panel but it don't should be. It's only visible if i move mouse pointer through place of this button.

推荐答案

,学习使用的布局,以你的优势。下面的示例使用了一系列嵌套布局到里面添加另一个一格。

Instead of setLayout(null), learn to use layouts to your advantage. The example below uses a series of nested layouts to add a one grid inside another.

图像

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * @see http://stackoverflow.com/a/36243395/230513
 */
public class Test {

    private static final int ROW = 2;
    private static final int COL = 5;

    private void display() {
        JFrame f = new JFrame("Test");
        f.setLayout(new GridLayout(0, 1));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel top = new JPanel(new GridBagLayout());
        top.setBackground(Color.darkGray);
        JLabel label = new JLabel("Post no bills.");
        label.setForeground(Color.yellow);
        top.add(label);
        f.add(top);
        f.add(createGridPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JPanel createGridPanel() {
        JPanel p = new JPanel(new GridLayout(ROW, COL, 5, 5));
        p.setBorder(BorderFactory.createLineBorder(Color.yellow,5));
        p.setBackground(Color.yellow);
        for (int r = 0; r < ROW; r++) {
            for (int c = 0; c < COL; c++) {
                p.add(createSubPanel());
            }
        }
        return p;
    }

    private JPanel createSubPanel() {
        JPanel p = new JPanel(new GridLayout(0, 1));
        JPanel top = new JPanel();
        top.add(new JButton("One"));
        top.add(new JButton("Two"));
        JPanel bot = new JPanel();
        bot.add(new JRadioButton("A"));
        bot.add(new JRadioButton("B"));
        bot.add(new JRadioButton("C"));
        bot.add(new JRadioButton("D"));
        p.add(top);
        p.add(bot);
        return p;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

附录:我要......两个的JButton S代表向前和后退

要允许通过导航按钮从一个面板到另一个,使用 CardLayout ,显示这里及以下修改。

To permit navigation by button from one panel to another, use CardLayout, shown here and revised below.

图像

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * @see http://stackoverflow.com/a/36243395/230513
 */
public class CardPanel extends JPanel {

    private static final JPanel cards = new JPanel(new CardLayout());
    private final String name;

    public CardPanel(String name) {
        super(new GridLayout(0, 1));
        this.name = name;
        JPanel top = new JPanel(new GridBagLayout());
        top.setBackground(Color.darkGray);
        JLabel label = new JLabel(name);
        label.setForeground(Color.yellow);
        top.add(label);
        JPanel bot = new JPanel();
        bot.setBorder(BorderFactory.createLineBorder(Color.yellow, 5));
        bot.add(new JRadioButton("A"));
        bot.add(new JRadioButton("B"));
        bot.add(new JRadioButton("C"));
        bot.add(new JRadioButton("D"));
        this.add(top);
        this.add(bot);
    }

    @Override
    public String toString() {
        return name;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 1; i < 9; i++) {
            CardPanel p = new CardPanel("Panel " + String.valueOf(i));
            cards.add(p, p.toString());
        }
        JPanel control = new JPanel();
        control.add(new JButton(new AbstractAction("\u22b2Prev") {

            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.previous(cards);
            }
        }));
        control.add(new JButton(new AbstractAction("Next\u22b3") {

            @Override
            public void actionPerformed(ActionEvent e) {
                CardLayout cl = (CardLayout) cards.getLayout();
                cl.next(cards);
            }
        }));
        f.add(cards, BorderLayout.CENTER);
        f.add(control, BorderLayout.SOUTH);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

这篇关于的JButton上的JPanel其所不应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆