gpt4 book ai didi

java - 更改所有选项卡 Pane 面板 java swing 的颜色的按钮操作

转载 作者:行者123 更新时间:2023-12-02 08:01:13 24 4
gpt4 key购买 nike

我在选项卡 Pane 中有 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 构造函数添加了一个按钮,使所有 Pane 使用相同的颜色。

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

关于java - 更改所有选项卡 Pane 面板 java swing 的颜色的按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8880268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com