Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 图形用户界面_Java_User Interface - Fatal编程技术网

Java 图形用户界面

Java 图形用户界面,java,user-interface,Java,User Interface,所以,我试图在左右两侧各创建8个按钮。我对GUI真的很陌生。所以,我不知道如何改变颜色和形状,使这些按钮成为一个圆圈,并将它们涂成红色和蓝色…这就是我目前所拥有的 import javax.swing.*; 导入java.awt.* 公课安排{ // main must be static public static void main(String[] args) { Arrangement arrangement = new Arrangement(); arrangeme

所以,我试图在左右两侧各创建8个按钮。我对GUI真的很陌生。所以,我不知道如何改变颜色和形状,使这些按钮成为一个圆圈,并将它们涂成红色和蓝色…这就是我目前所拥有的

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

}
} 现在的问题是我不能用这个,使用static,如果我删除static,我会得到一个错误,说我需要包含static才能使代码正常工作。。。
有人能帮我调试一下吗?'并指导我如何使我的按钮达到所需的形状和颜色。。。!任何帮助都将不胜感激用户可以使用自定义图标作为按钮

jButton1.setIconnew javax.swing.ImageIconC:\\Users\\admin\\Desktop\\image.jpg

若要将颜色设置为按钮,请使用以下代码

    jButton1.setBackground(Color.BLUE);
    jButton1.setForeground(Color.GREEN);

希望我的代码能在这方面对您有所帮助。

这条评论太长,但它涉及到您对static关键字的使用:

您不能使用this关键字并且必须具有static的原因是因为您的代码是在static main方法中执行的。相反,将所有代码从main移动到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 */
    }

}
可能有帮助的其他问题:


编辑中描述了类似的任务。用户希望在屏幕上显示可点击的圆圈和正方形。他没有使用JButton,而是在屏幕上简单地绘制形状。

好吧,您的代码有三个问题。第一个问题已经讨论过了。不能从静态上下文引用非静态变量

这个词不是静态的,主要方法是静态的

现在,要更改背景色,可以对要更改颜色的元素调用setBackground方法

最后,要制作按钮的形状,您可以按照以下链接进行操作:

基本上,您在这里所做的是扩展jbutton类。不允许它绘制自己的形状并使用图形在其上绘制。 您可以使用图形在每个JavaGUI组件上绘制。您所要做的就是实现反绘制组件方法

顺便说一下,这是一种更清晰、更简单的显示图像图标的方法

最后一条注释,使调试更容易的关键是尝试编写干净且有组织的代码。 例如,我可以看到您一次又一次地重复以下代码:

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循环对其进行打包:

左侧按钮的包装:

for(int j=0;j<8;j++){
    pack(c, 0, j+1,buttons[j+1]);
}
这是最终结果

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

}

不能从静态上下文引用非静态变量。您可以做的是创建一个排列实例,并将其替换为您的实例reference@AbsalonCasta尼昂是对的。我在下面给出了关于这个问题的更多细节。不是真正的答案,但我建议您不要做awt。*;和摇摆。*;它们都是巨大的图书馆,当你不需要那里的所有东西时,它们可能会减慢程序的速度。谢谢你的建议@Jack:注意!当我尝试第一个建议时,它会给我一个空白屏幕,我猜setBackground应该设置blackground颜色,setForeground会因此改变按钮上文字的颜色。我想做的是改变整个按钮本身的颜色和形状。不管怎样,谢谢你的帮助…那么最好使用设置图标的方法创建你自己的.jpg文件,并使用上面的语法将它们放在按钮上,这对我来说真的很有用,也许你应该尝试一下,一旦我在这方面应该提醒你,图像不要让你的图像太大,为按钮使用合适大小的图像希望这有帮助!我调整了图像的大小!我不确定问题出在哪里。但我最终创造了新的形状。但是感谢您的输入::我非常感谢:d当我这样做时…它给我一个错误,说类型排列的方法句柄未定义…请确保保存文件并检查拼写。无所谓。是的,我知道了。除此之外,图像仍然无法显示在帧上。但是谢谢你的帮助:谢谢!你有什么错误?另外,我在我的帖子中添加了一个新链接,没有错误。只是,如果我创建了一个排列的实例,然后继续使用这个关键字,我所有的按钮都会消失,我的框架是空白的……哦,也谢谢你的链接!我会调查的!我已经编辑了关于第一期的代码,它已经被修复了!谢谢你的标签链接,我会看看的!此外,我并没有试图改变背景颜色,我试图改变按钮的颜色。谢谢tho:我明白了,每个组件都有一个背景色,所以如果你想改变按钮的颜色,你必须改变它的背景色非常感谢:D!是的,我也在尝试创建一个for循环,但一直都很混乱
谢谢你花这么多时间来解决我的问题!我非常感激:这就是社区的意义所在。我相信当轮到你的时候,你也会帮助别人
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);
}