如何在达到某个数字之前创建计数器

时间:2022-05-06 21:26:45

How can I create a program that reaches a certain number? I'm creating a flag using JPanels but i need to keep going until I reach 350000. This means having to change each number accordingly in each line. I want to create a program which gives me the numbers in the JPanel and setbackground line up until 350000. Also in the panes.

如何创建达到一定数量的程序?我正在使用JPanels创建一个标志,但我需要继续前进,直到达到350000.这意味着必须在每行中相应地更改每个数字。我想创建一个程序,它给我JPanel和setbackground中的数字直到350000。同样在窗格中。

so far (only 4);

到目前为止(只有4个);

import javax.swing.; import java.awt.;

import javax.swing。; import java.awt。;

public class Albania {

公共课阿尔巴尼亚{

public static void main(String[] args) {
    JFrame albania = new JFrame();
    albania.setTitle("Albania");
    albania.setSize(300, 200);
    albania.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel1 = new JPanel();
    panel1.setBackground(Color.red);
    JPanel panel2 = new JPanel();
    panel2.setBackground(Color.red);
    JPanel panel3 = new JPanel();
    panel3.setBackground(Color.red);
    JPanel panel4 = new JPanel();
    panel4.setBackground(Color.red);
    Container pane = albania.getContentPane();
    pane.setLayout(new GridLayout(2, 2));;
    pane.add(panel1);
    pane.add(panel2);
    pane.add(panel3);
    pane.add(panel4);
    albania.setVisible(true);

}

}

3 个解决方案

#1


1  

The answer for what you have asked is add panels in a loop

您所要求的答案是在循环中添加面板

     JFrame albania = new JFrame();
        albania.setTitle("Albania");
        albania.setSize(300, 200);
        albania.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container pane = albania.getContentPane();
        pane.setLayout(new GridLayout(2, 2));;

for(int i =0; i < 350000; i++){
     JPanel panel1 = new JPanel();
        panel1.setBackground(Color.red);
        JPanel panel2 = new JPanel();
     pane.add(panel1);
    }
        albania.setVisible(true);

if you only objective is create a colored grid, you should use JLabels instead of JPanels... JPanel is a more complex object and leading with so many concurrent instances of it will lead you to bad performance in your application

如果你只是目标是创建一个彩色网格,你应该使用JLabel而不是JPanels ... JPanel是一个更复杂的对象,并且领导着如此多的并发实例会导致你的应用程序性能不佳

#2


0  

This creates an ArrayList of 350,000 JPanel objects and adds them to your JFrame. As the commenter noted above, this is likely going to cause very poor performance if there is even enough memory to complete the operations.

这将创建一个包含350,000个JPanel对象的ArrayList,并将它们添加到您的JFrame中。正如上面提到的那样,如果有足够的内存来完成操作,这可能会导致非常糟糕的性能。

public static void main(String[] args) {
    JFrame albania = new JFrame();
    ArrayList<JPanel> panels = new ArrayList<>();
    albania.setTitle("Albania");
    albania.setSize(300, 200);
    albania.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel newPanel;
    for (int x = 0; x<350000; x++) {
        newPanel = new JPanel();
        newPanel.setBackGround(Color.red);
        panels.add(new JPanel());
    }

    Container pane = albania.getContentPane();
    pane.setLayout(new GridLayout(2, 2));

    for (int x = 0; x<350000; x++) {
        pane.add(panels.get(x));
    }
    albania.setVisible(true);

}

#3


0  

Like the others have suggested, a for loop is needed. However, you may need to access one of the 350,000 JPanels again. Although this will take up much memory, make an array.

像其他人建议的那样,需要一个for循环。但是,您可能需要再次访问350,000个JPanel之一。虽然这将占用大量内存,但要制作一个数组。

’JFrame albania = new JFrame();
    albania.setTitle("Albania");
    albania.setSize(300, 200);
albania.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Container pane = albania.getContentPane();
    pane.setLayout(new GridLayout(2, 2));
 JPanel[] panels = new JPanel()[350000];
 for(int i =0; i < 350000; i++){
 panels[i] = new JPanel();
     panels[i]setBackground(Color.red);
 pane.add(panels[i]);
}
    albania.setVisible(true)`

#1


1  

The answer for what you have asked is add panels in a loop

您所要求的答案是在循环中添加面板

     JFrame albania = new JFrame();
        albania.setTitle("Albania");
        albania.setSize(300, 200);
        albania.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       Container pane = albania.getContentPane();
        pane.setLayout(new GridLayout(2, 2));;

for(int i =0; i < 350000; i++){
     JPanel panel1 = new JPanel();
        panel1.setBackground(Color.red);
        JPanel panel2 = new JPanel();
     pane.add(panel1);
    }
        albania.setVisible(true);

if you only objective is create a colored grid, you should use JLabels instead of JPanels... JPanel is a more complex object and leading with so many concurrent instances of it will lead you to bad performance in your application

如果你只是目标是创建一个彩色网格,你应该使用JLabel而不是JPanels ... JPanel是一个更复杂的对象,并且领导着如此多的并发实例会导致你的应用程序性能不佳

#2


0  

This creates an ArrayList of 350,000 JPanel objects and adds them to your JFrame. As the commenter noted above, this is likely going to cause very poor performance if there is even enough memory to complete the operations.

这将创建一个包含350,000个JPanel对象的ArrayList,并将它们添加到您的JFrame中。正如上面提到的那样,如果有足够的内存来完成操作,这可能会导致非常糟糕的性能。

public static void main(String[] args) {
    JFrame albania = new JFrame();
    ArrayList<JPanel> panels = new ArrayList<>();
    albania.setTitle("Albania");
    albania.setSize(300, 200);
    albania.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel newPanel;
    for (int x = 0; x<350000; x++) {
        newPanel = new JPanel();
        newPanel.setBackGround(Color.red);
        panels.add(new JPanel());
    }

    Container pane = albania.getContentPane();
    pane.setLayout(new GridLayout(2, 2));

    for (int x = 0; x<350000; x++) {
        pane.add(panels.get(x));
    }
    albania.setVisible(true);

}

#3


0  

Like the others have suggested, a for loop is needed. However, you may need to access one of the 350,000 JPanels again. Although this will take up much memory, make an array.

像其他人建议的那样,需要一个for循环。但是,您可能需要再次访问350,000个JPanel之一。虽然这将占用大量内存,但要制作一个数组。

’JFrame albania = new JFrame();
    albania.setTitle("Albania");
    albania.setSize(300, 200);
albania.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   Container pane = albania.getContentPane();
    pane.setLayout(new GridLayout(2, 2));
 JPanel[] panels = new JPanel()[350000];
 for(int i =0; i < 350000; i++){
 panels[i] = new JPanel();
     panels[i]setBackground(Color.red);
 pane.add(panels[i]);
}
    albania.setVisible(true)`