gpt4 book ai didi

java - 使用 GridBagLayout JComponent 未在 JPanel 上显示

转载 作者:行者123 更新时间:2023-12-01 13:06:07 26 4
gpt4 key购买 nike

因此,经过一番研究后,我倾向于需要事先指定 JComponent 的首选大小,因为 JComponent 确实没有首选大小本质上。

向我的 JComponent 添加首选尺寸是否可以解决面板上显示的问题?

如果这是解决问题的方法,那么最好的方法是什么?我读过不同的人说使用 setPreferedSize 和其他人说你根本不应该使用此方法。

import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.ComponentOrientation;

public class ArrayViewer
{
static int[] array;
static int[] sortedArray;

public static void main(String[] args)
{
int size=0;
Scanner in=new Scanner(System.in);
//ask for the size of the array until the user enters a size in the right range
do
{
System.out.print("Enter the size of the array (should be between 10 and 100): ");
size=in.nextInt();
}
while (size<10 || size>100);

JFrame frame = new JFrame();
frame.setSize(1000,1000);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

array= ArrayUtil.randomIntArray(size,200);//create a random array of given size and entries ranging from 0 to 100
System.out.println(ArrayUtil.printArray(array));



//Set up the content pane.
addComponentsToPane(frame.getContentPane());

//Display the window.
frame.pack();
frame.setVisible(true);


}

public static void addComponentsToPane(Container pane)
{
pane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//For each component to be added to this container:

ArrayComponent arrayGraph = new ArrayComponent(array, false);
//...Set instance variables in the GridBagConstraints instance...
c.ipady = 0; //reset to default
c.weighty = 0.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(0,0,0,0); //no padding
c.gridx = 0; //begins in 1st cell of..
c.gridwidth = 2; //2 columns wide
c.gridy = 1; //2nd row
pane.add(arrayGraph, c);


}

ArrayComponent类

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JComponent;

public class ArrayComponent extends JComponent
{
// instance variables - replace the example below with your own
int[] theArray;
int highlightIndex;
boolean sorted;

/**
* Constructor for objects of class ArrayComponent
*/
public ArrayComponent(int[] input, boolean sorted)
{
// initialise instance variables
theArray = input;
this.sorted = sorted;
}

public void paintComponent(Graphics g)
{
if (sorted == false)
{
Graphics2D g2 = (Graphics2D)g;
int x = 25;
int size = theArray.length;
for(int i = 0; i<size; i++)
{

g2.drawRect(x,15,5,theArray[i]);
x = x+10;
}

}

}
}

ArrayUtil 类

public class ArrayUtil
{
private static Random generator = new Random();

/**
Creates an array filled with random values.
@param length the length of the array
@param n the number of possible random values
@return an array filled with length numbers between
0 and n - 1
*/
public static int[] randomIntArray(int length, int n)
{
int[] a = new int[length];
for (int i = 0; i < a.length; i++)
a[i] = generator.nextInt(n);

return a;
}

public static String printArray(int[] array)
{
String str=("array=[");
int k=0;
for (k=0;k<array.length-1;k++)
str=str+array[k]+", ";
str=str+array[k]+"]";
return str;
}

最佳答案

重写getPreferredSize而不是使用setPreferredSize

您还可以使用 GridBagLayout 通过指定 fill 属性并更改 weightx/y 来影响大小属性(property)

关于java - 使用 GridBagLayout JComponent 未在 JPanel 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23256115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com