簡體   English   中英

按鈕動作改變所有tabbedpane面板java swing的顏色

[英]Button action to change the color of all tabbedpane panel java swing

tabbedpane中有2個標簽(A和B)。 在A中,我只寫了setBackground(Color.RED);

在B中,我放了一個按鈕。 並且代碼喜歡:

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);
        }
    });

引用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));
            }
        }
    }));
}

看看你的代碼,你似乎做錯了。 首先不要寫這些行

private JPanel panel1;
private JPanel panel2;

改為寫:

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

由於a和b,它們本身就是Panels,因為它們擴展了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");

只需在您的B類中添加一個JPanel變量,然后按如下方式更改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的背景顏色更改為藍色時,這是第一個選項卡的圖像:

背景顏色改變了TAB1的顏色

希望這可以幫助你的努力。

問候

您不應創建A的新實例,而應使用選項卡式窗格中包含的A實例。 您可以到這個通過搜索選項卡窗格此A實例,或通過A實例來B創建時, B

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM