所以,我尝试在左边和右边创建按钮8。我对GUI非常陌生。因此,我不知道如何改变颜色和形状,如何使这些按钮成一个圆圈,并将它们涂成红色和blue...This,这是我目前所拥有的……
import javax.swing.*;进口java.awt.*;
公共班级安排{
// main must be static
public static void main(String[] args) {
Arrangement arrangement = new Arrangement();
arrangement.handle();
}
public void handle() {
JFrame f= new JFrame();
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel (new GridBagLayout());//in the constructor u specify the layout :)
JPanel a = new JPanel (new GridBagLayout());
JButton b1 = new JButton("Button 1");
JButton b2 = new JButton("Button 2");
JButton b3= new JButton ("Button 3");
JButton b4= new JButton ("Button 4");
JButton b5= new JButton ("Button 5");
JButton b6= new JButton ("Button 6");
JButton b7 = new JButton("Button 7");
JButton b8 = new JButton("Button 8");
JButton b9 = new JButton ("Button 9");
JButton b10 = new JButton ("Button 10");
JButton b11 = new JButton ("Button 11");
JButton b12 =new JButton ("Button 12");
JButton b13 = new JButton("Button 13");
JButton b14= new JButton("Button 14");
JButton b15= new JButton ("Button 15");
JButton b16 = new JButton ("Button 16");
GridBagConstraints c= new GridBagConstraints();
GridBagConstraints d= new GridBagConstraints();
c.insets = new Insets(5,5,5,5);//spacing
c.gridx=0;
c.gridy=1;
p.add(b1,c);
c.gridx=0;
c.gridy=2;
p.add(b2,c);
c.gridx=0;
c.gridy=4;
p.add(b3,c);
c.gridx=0;
c.gridy=6;
p.add(b4,c);
c.gridx=0;
c.gridy=8;
p.add(b5,c);
c.gridx=0;
c.gridy=10;
p.add(b6,c);
c.gridx=0;
c.gridy=11;
p.add(b7,c);
c.gridx=0;
c.gridy=12;
p.add(b8,c);
d.insets = new Insets(5,5,5,5);
d.gridx=0;
d.gridy=1;
a.add(b9,d);
d.gridx=0;
d.gridy=2;
a.add(b10,d);
d.gridx=0;
d.gridy=3;
a.add(b11,d);
d.gridx=0;
d.gridy=4;
a.add(b12,d);
d.gridx=0;
d.gridy=6;
a.add(b13,d);
d.gridx=0;
d.gridy=8;
a.add(b14,d);
d.gridx=0;
d.gridy=10;
a.add(b15,d);
d.gridx=0;
d.gridy=12;
a.add(b16,d);
f.add(p, BorderLayout.WEST);
f.add(a, BorderLayout.EAST);
}}现在这里的问题是我不能使用"this ",对于静态,如果我删除了静态,我会收到一个错误,说我需要包含静态代码才能工作.有人能帮我调试一下这个吗?(并指导我如何才能得到我想要的按钮的形状和颜色……!如有任何帮助,我们将不胜感激。
发布于 2016-02-15 15:16:54
嗯,code.The有三个问题,第一个问题已经讨论过了。不能引用静态上下文中的非静态变量。
这个词不是静态的,主要的方法是静态的。
现在,要更改背景色,可以有效地对要更改颜色的元素调用setBackground()方法。
最后,要使按钮的形状,您可以遵循以下链接:Making buttons round
糟糕的是,您在这里所做的是扩展jbutton类。不允许它绘制自己的形状并使用图形在其上绘图。您可以使用图形来绘制每个java组件。您所要做的就是实现方法。
顺便说一句,here是一种更清晰、更简单的显示图像图标的方法。
最后一个注释,让调试变得更容易的关键是尝试编写干净和有组织的代码。例如,我可以看到您一次又一次地重复以下代码:
c.gridx=0;
c.gridy=1;
p.add(b1,c);您可以做的是创建一个函数来打包它。
void pack(Insets target, int xCoord,int yCoord,Component comp ){
target.gridx=xCoord;
target.gridy=yCoord;
p.add(comp, target);
}您也可以创建一个包含所有按钮的数组,因此可以使用for循环来pack:
JButton[] buttons= new JButton[8];
for(int j=0;j<16;j++){
buttons[j]= new JButton("Button "+(j+1));
}左按钮的包装:
for(int j=0;j<8;j++){
pack(c, 0, j+1,buttons[j+1]);
}Thi是最终的结果
import javax.swing.*;
import java.awt.*;
import java.awt.*;
import java.awt.image.*;
public class Arrangement {
public void main(String[] args) {
JFrame f= new JFrame();
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel (new GridBagLayout());//in the constructor u specify the layout :)
JPanel a = new JPanel (new GridBagLayout());
Arrangement arr= new Arrangement();
for(int j=0;j<16;j++){
if(j==0)
Image img= new ImageIcon(arr.getClass().getResource("/Red.png")).getImage();
//Supposing you followed the link and created the class
buttons[j]= new RoundButton("Button "+(j+1));
}
GridBagConstraints c= new GridBagConstraints();
GridBagConstraints d= new GridBagConstraints();
c.insets = new Insets(5,5,5,5);//spacing
for(int j=0;j<8;j++){
arr.pack(c, 0, j+1,buttons[j],p);
}
d.insets = new Insets(5,5,5,5);
for(int j=0;j<8;j++){
arr.pack(d, 0, j+9,buttons[j+8],a);
}
f.add(p, BorderLayout.WEST);
f.add(a, BorderLayout.EAST);
}
void pack(Insets target, int xCoord,int yCoord,Component comp, Panel container ){
target.gridx=xCoord;
target.gridy=yCoord;
container.add(comp, target);
}}
发布于 2016-02-15 14:39:59
这对评论来说太长了,但它与您使用static关键字有关:
您不能使用this关键字并必须拥有static的原因是您的代码是在静态main方法中执行的。相反,将所有代码从main移到Arrangement类中的一个新方法,比如它称为handle()。然后在主类的开头创建一个Arrangement实例,并调用handle()。例如:
public class Arrangement {
// main must be static
public static void main(String[] args) {
Arrangement arrangement = new Arrangement();
arrangement.handle();
}
public void handle() {
/* Put the rest of your code here and
* you'll be able to use the 'this' keyword */
}
}可能有帮助的其他问题:
编辑在this question中描述了类似的任务。用户希望在屏幕上显示可点击的圆圈和方块.他没有使用JButton,而是简单地在屏幕上绘制形状。
发布于 2016-02-15 14:28:53
可以为按钮使用自定义图标,如
jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\admin\\Desktop\\image.jpg"));
如果要将颜色设置为按钮,请使用以下代码
jButton1.setBackground(Color.BLUE);
jButton1.setForeground(Color.GREEN);希望我的代码在这方面对你有帮助。
https://stackoverflow.com/questions/35411736
复制相似问题