简体   繁体   English

图形用户界面

[英]Graphical User Inerface

So, I am trying to create buttons 8 on left and right side each. 因此,我试图在左侧和右侧分别创建按钮8。 And I am really new to GUI. 而且我真的是GUI的新手。 So, I am not sure how to change the colour and shape, to make those buttons a circle and color them in red and blue...This what I have so far... 因此,我不确定如何更改颜色和形状,将那些按钮变成一个圆圈并用红色和蓝色进行着色...这是我到目前为止所拥有的...

import javax.swing.*;

import java.awt.*; 导入java.awt。*;

public class Arrangement { 公共课程安排{

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

}

} Now the problem here is I cant use "this. ", with static and if I remove static I get an error saying I need to include static for my code to work... Can someone help me debug this :'( and direct me as to how can I get my buttons to be desired shape and colour...! Any help would be greatly appreciated <3 }现在,这里的问题是我不能在静态中使用“ this。”,如果我删除了静态,我会收到一条错误消息,说我需要包括静态才能使我的代码正常工作...有人可以帮我调试它吗关于如何使按钮达到理想的形状和颜色...!任何帮助将不胜感激<3

This was too long for a comment, but it relates to your use of the static keyword: 这个评论太长了,但与您使用static关键字有关:

The reason you can't use the this keyword and must have static is because your code is executed in the static main method. 您不能使用this关键字并且必须具有static的原因是因为您的代码是在静态main方法中执行的。 Instead, move all the code from main to a new method in the Arrangement class, say it's called handle() . 相反,将所有代码从main移到Arrangement类中的新方法,称其为handle() Then create an instance of Arrangement at the beginning of your main class and call handle() . 然后在主类的开头创建一个Arrangement实例,并调用handle() For instance: 例如:

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 */
    }

}

Additional questions that might be helpful: 其他可能有用的问题:

EDIT A similar task was described in this question . 编辑这个问题中描述了类似的任务。 The user wanted to have clickable circles and squares display on-screen. 用户希望在屏幕上显示可点击的圆形和正方形。 Rather than using a JButton he simply drew shapes on-screen. 与其使用JButton他只是在屏幕上绘制形状。

use can use custom icons for your button as 使用可以使用自定义图标作为按钮

jButton1.setIcon(new javax.swing.ImageIcon("C:\\\\Users\\\\admin\\\\Desktop\\\\image.jpg"));

if you want to set color to button then use following code 如果要将颜色设置为按钮,请使用以下代码

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

hope my code helps you in this regard. 希望我的代码在这方面对您有所帮助。

Well, there are three issues with your code.The first one is already discussed. 好了,您的代码存在三个问题。第一个已经讨论过了。 You can't reference non static variables from a static context. 您不能从静态上下文引用非静态变量。

this word is not static and main method is static 这个词不是静态的,主要方法是静态的

Now, to change background color, effectively you call the setBackground() method on the element you want the color to change. 现在,要更改背景颜色,可以有效地在要更改颜色的元素上调用setBackground()方法。

Finally, to make the shape of the button you can follow this link: Making buttons round 最后,要制作按钮的形状,您可以点击以下链接: 使按钮变圆

Bassically what you are doing here is extending the jbutton class. 基本上,您在这里所做的就是扩展jbutton类。 Not allowing it to draw its own shape and use graphics to draw on it. 不允许其绘制自己的形状并使用图形在其上绘制。 You can use graphics to draw on every java GUI componnent. 您可以使用图形在每个Java GUI组件上绘制。 All you have to do is to implement de paint componnent method 您所要做的就是实现脱漆组分方法

By the way, here is a clearer and simpler way to display image icons. 顺便说一下, 是一种更清晰,更简单的显示图像图标的方法。


One last comment, the key to make it easier to debug is trying to write clean and organized code. 最后一句话,使调试更容易的关键是尝试编写干净且有组织的代码。 For example, I can see that you repeat the following code once and again: 例如,我可以看到您一次又一次重复以下代码:

c.gridx=0;
c.gridy=1;
p.add(b1,c);

What you can do is create a function to pack it 您可以做的是创建一个函数来打包它

void pack(Insets target, int xCoord,int yCoord,Component comp ){
target.gridx=xCoord;
target.gridy=yCoord;
p.add(comp, target);
}

you can create an array with all the buttons too, so you can use a for loop to pack them: 您也可以使用所有按钮创建一个数组,因此可以使用for循环来打包它们:

JButton[] buttons= new JButton[8];

for(int j=0;j<16;j++){
    buttons[j]= new JButton("Button "+(j+1));
}

Packing of left buttons: 左键包装:

for(int j=0;j<8;j++){
    pack(c, 0, j+1,buttons[j+1]);
}

Thi is the final result 这是最终结果

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM