简体   繁体   中英

how to make JButtons in java with less code?

I am creating a program for a personal project and I ended up creating many JButtons, which are taking up quite bit of lines in the project, is it possible to reduce the amount of lines that still produces that same amount of JButton?

JButton button1 = new JButton("Empty"); 
JButton button2 = new JButton("Empty"); 
JButton button3 = new JButton("Empty"); 
JButton button4 = new JButton("Empty"); 
JButton button5 = new JButton("Empty"); 
JButton button6 = new JButton("Empty"); 
JButton button7 = new JButton("Empty"); 
JButton button8 = new JButton("Empty"); 
JButton button9 = new JButton("Empty"); 
JButton button10 = new JButton("Empty"); 
JButton button11 = new JButton("Empty"); 
and about 5 more

For repetitive tasks with similar functionality, I like to use methods. Most of the time it will make the code cleaner. It won't shorten the current code you have, but after you add listener or anything else you may want to add, it will. You can pass any number of variables you want to it.

public JButton createButton(String name, Color color) {
    JButton button = new JButton(name);
    button.setForeground(color);
    button.addActionListener(listener);
    return button;
}

Then you can just call it a bunch of times like

JButton button = createButton("Hello", Color.BLUE);

Overall though it's really a case by case basis on how you create your components. If you concern is really to shorten your code. You can always use loops, and some data structure for your the names

String[] names = { "Hey", "Ya", "Yada" };
JButton[] buttons = new JButton[names.lenght];
for (int i = 0; i < buttons.length; i++) {
    buttons[i] = createButton(names[i], Color.BLUE);
} 

Maybe even look at some of the answers from this question

U can create button with loops

Vector<Button> lists = new Vector<Button>();

for(int i = 0 ; i < 10000 ; i++) {
  JButton button = new JButton("Empty"); 
  lists.add(button);
}

And more, more.. other way

JButton[] buttons = new JButton[n];

int x=0;
while(x<n){

buttons[n]= new JButton("Button");
x++;
}

While n is the number of buttons you want

EDIT

If you want to give Separate Names to Buttons Like button1, 2 ,3

int x=0;
while(x<n){

buttons[n]= new JButton("Button "+(x+1));   //x+1 if you want to start from 1, because x is 0
x++;
}

As others have noted, you can make a loop and add buttons, to an array, but Java doesn't support macros [1] , which seems to be the thing you wish to do (that is have the code process automated, but still retain sensible variable names).

Note: While it's possible to hack some kind of simple macro format in Java but I'd advice not to.

Your best bet would be to look into code generators that generate the Java - this might be a good starting point.

For generating code I just use FreeMarker Template. It's not perfect, but it's decent. Basically you'll have some kind of program (Java program, Ant task, etc.) that will call FreeMarker libraries, give them templates and data, in return that will generate the expected code.

import java.awt.Component;
import java.awt.Container;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ComponentFactory {

  public static void main(String[] args) {
    Box box=Box.createVerticalBox();
    addAll(box, createButtons("foo", "bar", "baz")); // <----------
    JFrame f=new JFrame("Example");
    f.setContentPane(box);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }

  static void addAll(Container target, Component... all) {
    for(Component c:all) target.add(c);
  }

  static JButton[] createButtons(String... label) {
    final int num=label.length;
    JButton[] all=new JButton[num];
    for(int i=0; i<num; i++) all[i]=new JButton(label[i]);
    return all;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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