【问题标题】:Button action to change the color of all tabbedpane panel java swing改变所有tabbedpane面板颜色的按钮动作java swing
【发布时间】:2012-02-11 09:49:35
【问题描述】:

我在选项卡窗格中有 2 个选项卡(A 和 B)。 在A中,我只写了setBackground(Color.RED);

在 B 中,我放了一个 Button。并且代码是这样的:

A a=new A();

jButton1.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    a.setBackground(Color.BLUE);
    }
});

我想通过 B 的按钮动作改变 A 的颜色。但我失败了。 我该如何解决这个问题??

提前谢谢...


我的问题仍然没有解决。我发布了整个代码:: 我使用了 2 个包:“ok”、“ok1”。 "ok" 包含 1 个名为 save.java 的文件,代码为:

public class Save extends javax.swing.JFrame {
private JPanel panel1;
private JPanel panel2;A a=new A();B b=new B();



public Save() {
    initComponents();
}

//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
    panel1=a.initComponents();
    panel2=b.initComponents();
    jTabbedPane1 = new javax.swing.JTabbedPane();
    jScrollPane1 = new javax.swing.JScrollPane();
    jScrollPane2 = new javax.swing.JScrollPane();
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jTabbedPane1.addTab("Tab 1", null, panel1, "Just Panel");
    jTabbedPane1.setMnemonicAt(0, KeyEvent.VK_1);

    jTabbedPane1.addTab("Tab 2", null, panel2, "Button");
            jTabbedPane1.setMnemonicAt(1, KeyEvent.VK_2);

“ok1”包含2个文件:A.java和B.java..... A.java:::::::::

             public class A extends javax.swing.JPanel {

/** Creates new form A */
public A() {
    initComponents();

}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
public JPanel initComponents() {

    jPanel11 = new javax.swing.JPanel();

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
            jPanel11);
            jPanel11.setLayout(jPanel1Layout);

B.java::::::::

             public class B extends javax.swing.JPanel {
A a = new A();

/** Creates new form B */
public  B() {
    initComponents();


}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
public JPanel initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();

    jButton1.setText("Action");
    jButton1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            a.jPanel11.setBackground(Color.RED);
        }
    });

【问题讨论】:

  • 请附上一个sscce 来证明您的问题。
  • 感谢您的回复。能给我举个SSCCE的例子吗???
  • 这是不言自明的,但这里有一个example
  • @Rounak :您正在以错误的方式进行编码。我正在更新我的答案,这类似于您的代码。看看吧。问候

标签: java swing background-color jbutton jtabbedpane


【解决方案1】:

参考TabColors,它以匹配选项卡和内容颜色开始,以下修改后的TabContent 构造函数添加了一个按钮,使所有窗格使用相同的颜色。

private TabContent(final int i, Color color) {
    setOpaque(true);
    setBackground(color);
    add(new JLabel("Tab content " + String.valueOf(i)));
    add(new JButton(new AbstractAction("Use my color") {

        @Override
        public void actionPerformed(ActionEvent e) {
            for (int j = 0; j < pane.getTabCount(); j++) {
                pane.getComponentAt(j).setBackground(
                    pane.getBackgroundAt(i));
            }
        }
    }));
}

【讨论】:

    【解决方案2】:

    查看您的代码,您似乎做错了。首先不要写这几行

    private JPanel panel1;
    private JPanel panel2;
    

    改为写:

    private A a = new A();
    private B b = new B(a);
    

    从 a 和 b 开始,它们本身就是面板,因为它们扩展了 JPanel 类。

    所以现在将它添加到您的 tabbedPane 中:

    jTabbedPane1.addTab("Tab 1", null, a/*This is your Panel1*/, "Just Panel");
    jTabbedPane1.addTab("Tab 2", null, b/*This is your Panel2*/, "Button");
    

    只需将 JPanel 变量添加到您的 B 类并更改您的 B 类的构造函数,如下所示:

    JPanel panel1;
    
    public B(JPanel panel)
    {
        pane1 = panel;
        initComponents(); // make this method return void in it's definition, in both the classes.
    }
    

    现在在 actionPerformed() 方法中执行以下操作:

    jButton1.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                panel1.setBackground(Color.RED);
            }
        });
    

    这是从之前提交的修改的小程序,类似于你的情况:

    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class TabbedPaneExample extends JPanel
    {
        private Panel1 panel1;
        private Panel2 panel2;
    
        public TabbedPaneExample()
        {
            super(new GridLayout(1, 1));
    
            JTabbedPane tabbedPane = new JTabbedPane();
    
            //panel1 = getPanel("Panel Number 1");  
            panel1 = new Panel1("Panel Number 1");
            tabbedPane.addTab("Tab 1", null, panel1, "Just Panel");
            tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
    
            //panel2 = getPanelWithButton("COLOR");
            panel2 = new Panel2("COLOR", panel1);
            tabbedPane.addTab("Tab 2", null, panel2, "Button");
            tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
    
            add(tabbedPane);
    
            tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        }
    
        private static void createAndDisplayGUI()
        {
            JFrame frame = new JFrame("Tabbed Pane Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(new TabbedPaneExample(), BorderLayout.CENTER);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        createAndDisplayGUI();
                    }
                });
        }
    }
    
    class Panel1 extends JPanel
    {
        public JLabel label;
        public Panel1(String text)
        {       
            label = new JLabel(text);
            label.setHorizontalAlignment(JLabel.CENTER);
            setLayout(new GridLayout(1, 1));
            setBackground(Color.RED);
            add(label);
        }
    }
    
    class Panel2 extends JPanel
    {
        public JButton button;
        public JPanel panel1;
    
        public Panel2(String text, JPanel panel)
        {
                panel1 = panel;
            button = new JButton(text);
            button.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ae)
                    {
                        panel1.setBackground(Color.BLUE);
                    }
                });
            setLayout(new GridLayout(1, 1));
            add(button);
        }
    }
    

    希望这能帮助你解释你做错了什么。

    这是程序启动时的图像:

    这是带有按钮的第二个标签的图像:

    这是第一个选项卡的图像,当您单击 tab2 的按钮以将 tab1 的背景颜色更改为蓝色时:

    希望这对您的努力有所帮助。

    问候

    【讨论】:

      【解决方案3】:

      您不应创建A 的新实例,而应使用选项卡式窗格中包含的A 实例。您可以通过在选项卡式窗格中搜索此A 实例来实现这一点,或者在创建B 时将A 实例传递给B

      【讨论】:

      • 对不起,我不明白该怎么做。
      • A a = new A() 不应出现在 B 类的代码中。发布 SSCCE,我们可以指出您哪里出错了
      猜你喜欢
      • 2011-03-26
      • 2012-01-07
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 2018-02-24
      • 1970-01-01
      • 2017-06-21
      相关资源
      最近更新 更多