背景图像隐藏了其他组件,如按钮标签等,反之亦然
background image hidding the other components like buttons labels and other, and vicce versa
- 如何解决这段代码中隐藏组件的问题
- 代码 运行 没有错误
- 但是没有显示背景图片
- 如何修改代码获取背景图片
当使用验证方法时,它在 validation() 中创建错误
public class TEST{
public TEST() {
String[] strm = {"Jan", "Feb", "Mar", "Apr", "May"};
String[] stry = {"2016", "2017", "2018","2019"};
String[] strf={"NEW Delhi", "Bangalore", "Chennai"};
String[] strt={"Goa","Kashmir","Hyderabad"};
JFrame f = new JFrame("test");
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lfr = new JLabel("FROM");
JLabel lto = new JLabel("TO");
JLabel lda = new JLabel("DATE");
JLabel ld = new JLabel("DAY");
JLabel lm = new JLabel("MONTH");
JLabel y = new JLabel("YEAR");
JComboBox cfr = new JComboBox(strf);
JComboBox cto = new JComboBox(strt);
JComboBox cd = new JComboBox();
JComboBox cm = new JComboBox(strm);
JComboBox cy = new JComboBox(stry);
JButton bs = new JButton("Search");
JPanel p1 = new JPanel(null);
p1.setPreferredSize(new Dimension(500,500));
JLabel jimage = new JLabel();
jimage.setIcon(new ImageIcon("air.jpg"));
for(int i = 1; i <= 31; i++)
cd.addItem(i);
lfr.setBounds(20, 40, 100, 20);
cfr.setBounds(100, 40, 100, 20);
lto.setBounds(20, 100, 25, 20);
cto.setBounds(100, 100, 100, 20);
lda.setBounds(20, 160, 50, 20);
cd.setBounds(100, 160, 50, 20);
cm.setBounds(160, 160, 65, 20);
cy.setBounds(240, 160, 75, 20);
ld.setBounds(100, 190, 50, 20);
lm.setBounds(160, 190, 50, 20);
y.setBounds(240, 190, 50, 20);
bs.setBounds(20, 230, 100, 20);
p1.add(lfr);
p1.add(cfr);
p1.add(lto);
p1.add(cto);
p1.add(lda);
p1.add(cd);
p1.add(cm);
p1.add(cy);
p1.add(ld);
p1.add(lm);
p1.add(y);
p1.add(bs);
p1.add(jimage);
// validate();
f.add(p1);
f.setVisible(true);
}
public static void main(String[] args) {
new TEST();
}
}
你能做的最好的事情是:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
JLabel label;
JComboBox combo;
JButton button;
JPanel pane;
JFrame frame;
JPanel create1stRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"New Delhi", "Bangalore", "Chennai"};
label = new JLabel("FROM");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create2ndRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"Goa", "Kashmir", "Hyderabad"};
label = new JLabel("TO");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create3rdRow() {
JPanel pane = new JPanel();
JPanel dataPane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
dataPane.setOpaque(false); //forgot to add this line when I took the pic
dataPane.setLayout(new GridLayout(2, 3)); //2 rows, 3 cols, so we can have the combos with their text aligned
String days[] = {"1", "2", "3", "4", "5"}; //Too lazy to write 31 days
String months[] = {"Jan", "Feb", "Mar", "Apr", "May"}; //Too lazy to write 12 months
String years[] = {"2016", "2017", "2018", "2019", "2020"};
label = new JLabel("DATE");
combo = new JComboBox(days);
//We add the combos
dataPane.add(combo);
combo = new JComboBox(months); //We're reusing the object, but change the data
dataPane.add(combo);
combo = new JComboBox(years); //The same as above
dataPane.add(combo);
//Now we add the labels
dataPane.add(new JLabel("DAYS"));
dataPane.add(new JLabel("MONTHS"));
dataPane.add(new JLabel("YEARS"));
pane.add(label);
pane.add(dataPane); //We add the whole pane to another one
return pane;
}
JPanel create4thRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
button = new JButton("Search");
pane.add(button);
return pane;
}
public Test() {
frame = new JFrame("Test");
frame.setContentPane(new JLabel(new ImageIcon("C:/Users/Frakcool/workspace/WhosebugProjects/src/test/Air.jpg")));
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
pane = create1stRow();
frame.add(pane);
pane = create2ndRow();
frame.add(pane);
pane = create3rdRow();
frame.add(pane);
pane = create4thRow();
frame.add(pane);
frame.pack();
//frame.setSize(500, 500); //If your image is too large use this
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[]) {
new Test();
}
}
正如你在上面的代码中看到的,我没有使用一个 null layout
而是多个 Layout Managers 的组合,我建议你以后这样做。
但如果您仍想使用那个丑陋的 null 布局,您就错过了这一行:
jimage.setBounds(0, 0, 500, 500);
在此之前:
lfr.setBounds(20, 40, 100, 20);
我上面的代码给出的输出是:
你的代码用我添加的行给出的输出是:
如您所见,两者非常相似,我本可以完全相同,但我没有足够的时间这样做,但您可以结合我在上面发布的布局管理器。
注意:我忘了说要让这个程序显示背景图片,我需要用 pane.setOpaque(false);
使每个其他面板不透明所以,每当您需要显示另一个面板后面的内容时,请务必使用它。
- 如何解决这段代码中隐藏组件的问题
- 代码 运行 没有错误
- 但是没有显示背景图片
- 如何修改代码获取背景图片
当使用验证方法时,它在 validation() 中创建错误
public class TEST{ public TEST() { String[] strm = {"Jan", "Feb", "Mar", "Apr", "May"}; String[] stry = {"2016", "2017", "2018","2019"}; String[] strf={"NEW Delhi", "Bangalore", "Chennai"}; String[] strt={"Goa","Kashmir","Hyderabad"}; JFrame f = new JFrame("test"); f.setSize(500, 500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel lfr = new JLabel("FROM"); JLabel lto = new JLabel("TO"); JLabel lda = new JLabel("DATE"); JLabel ld = new JLabel("DAY"); JLabel lm = new JLabel("MONTH"); JLabel y = new JLabel("YEAR"); JComboBox cfr = new JComboBox(strf); JComboBox cto = new JComboBox(strt); JComboBox cd = new JComboBox(); JComboBox cm = new JComboBox(strm); JComboBox cy = new JComboBox(stry); JButton bs = new JButton("Search"); JPanel p1 = new JPanel(null); p1.setPreferredSize(new Dimension(500,500)); JLabel jimage = new JLabel(); jimage.setIcon(new ImageIcon("air.jpg")); for(int i = 1; i <= 31; i++) cd.addItem(i); lfr.setBounds(20, 40, 100, 20); cfr.setBounds(100, 40, 100, 20); lto.setBounds(20, 100, 25, 20); cto.setBounds(100, 100, 100, 20); lda.setBounds(20, 160, 50, 20); cd.setBounds(100, 160, 50, 20); cm.setBounds(160, 160, 65, 20); cy.setBounds(240, 160, 75, 20); ld.setBounds(100, 190, 50, 20); lm.setBounds(160, 190, 50, 20); y.setBounds(240, 190, 50, 20); bs.setBounds(20, 230, 100, 20); p1.add(lfr); p1.add(cfr); p1.add(lto); p1.add(cto); p1.add(lda); p1.add(cd); p1.add(cm); p1.add(cy); p1.add(ld); p1.add(lm); p1.add(y); p1.add(bs); p1.add(jimage); // validate(); f.add(p1); f.setVisible(true); } public static void main(String[] args) { new TEST(); } }
你能做的最好的事情是:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test {
JLabel label;
JComboBox combo;
JButton button;
JPanel pane;
JFrame frame;
JPanel create1stRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"New Delhi", "Bangalore", "Chennai"};
label = new JLabel("FROM");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create2ndRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
String options[] = {"Goa", "Kashmir", "Hyderabad"};
label = new JLabel("TO");
combo = new JComboBox(options);
pane.add(label);
pane.add(combo);
return pane;
}
JPanel create3rdRow() {
JPanel pane = new JPanel();
JPanel dataPane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
dataPane.setOpaque(false); //forgot to add this line when I took the pic
dataPane.setLayout(new GridLayout(2, 3)); //2 rows, 3 cols, so we can have the combos with their text aligned
String days[] = {"1", "2", "3", "4", "5"}; //Too lazy to write 31 days
String months[] = {"Jan", "Feb", "Mar", "Apr", "May"}; //Too lazy to write 12 months
String years[] = {"2016", "2017", "2018", "2019", "2020"};
label = new JLabel("DATE");
combo = new JComboBox(days);
//We add the combos
dataPane.add(combo);
combo = new JComboBox(months); //We're reusing the object, but change the data
dataPane.add(combo);
combo = new JComboBox(years); //The same as above
dataPane.add(combo);
//Now we add the labels
dataPane.add(new JLabel("DAYS"));
dataPane.add(new JLabel("MONTHS"));
dataPane.add(new JLabel("YEARS"));
pane.add(label);
pane.add(dataPane); //We add the whole pane to another one
return pane;
}
JPanel create4thRow() {
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout(FlowLayout.LEFT));
pane.setOpaque(false);
button = new JButton("Search");
pane.add(button);
return pane;
}
public Test() {
frame = new JFrame("Test");
frame.setContentPane(new JLabel(new ImageIcon("C:/Users/Frakcool/workspace/WhosebugProjects/src/test/Air.jpg")));
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
pane = create1stRow();
frame.add(pane);
pane = create2ndRow();
frame.add(pane);
pane = create3rdRow();
frame.add(pane);
pane = create4thRow();
frame.add(pane);
frame.pack();
//frame.setSize(500, 500); //If your image is too large use this
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[]) {
new Test();
}
}
正如你在上面的代码中看到的,我没有使用一个 null layout
而是多个 Layout Managers 的组合,我建议你以后这样做。
但如果您仍想使用那个丑陋的 null 布局,您就错过了这一行:
jimage.setBounds(0, 0, 500, 500);
在此之前:
lfr.setBounds(20, 40, 100, 20);
我上面的代码给出的输出是:
你的代码用我添加的行给出的输出是:
如您所见,两者非常相似,我本可以完全相同,但我没有足够的时间这样做,但您可以结合我在上面发布的布局管理器。
注意:我忘了说要让这个程序显示背景图片,我需要用 pane.setOpaque(false);
使每个其他面板不透明所以,每当您需要显示另一个面板后面的内容时,请务必使用它。