gpt4 book ai didi

java - super 油漆组件没有出现(我已经尝试了一切)

转载 作者:行者123 更新时间:2023-12-01 22:33:42 25 4
gpt4 key购买 nike

大家好,我目前在使用我的绘画组件进行任何显示时遇到问题。我已经尝试了很多我在网上看到的东西,也尝试了很多不同的东西,但我就是无法让它显示任何东西。我试图让我的绘画组件显示一系列条形图,这些条形图作为使用 swing 排序的数组移动,但我什至无法让它显示任何内容。任何帮助将不胜感激。

package proj2;
import java.awt.*;
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;


public class project2 extends JFrame
{
private JButton button1 = new JButton("Insertion");
private UpdateTextFieldThread currentThread;
private int[] array = new int[15];
private Display display;

private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
if (src == button1){
insertionSort(array);
(new UpdateTextFieldThread()).execute();
}
}
}

public project2()
{
setTitle("Sorting Charts");
setSize(400,250);
setDefaultCloseOperation(EXIT_ON_CLOSE);

for(int i=0; i<array.length;i++)
array[i]=(int) (Math.random()*100);
display=new Display();

ButtonHandler bh = new ButtonHandler();
button1.addActionListener(bh);

JPanel container = new JPanel();
container.setLayout(new GridLayout(2, 2));
container.add(button1);

JPanel masterPanel=new JPanel();
masterPanel.setLayout(new BorderLayout());
masterPanel.add(display, BorderLayout.SOUTH);
masterPanel.add(container, BorderLayout.NORTH);

setContentPane(container);
setVisible(true);
display.repaint();
}

private class Display extends JPanel {
private Color color = Color.RED;

//@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g.setColor(Color.RED);
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / array.length;
for(int i=0;i<array.length;i++){
int x=0;
int y=15;
int linethickness=5;
int linelength=10;
g.setColor(Color.red);
g.fillRect(x, clientHeight, linelength*array[i] , linethickness);
g.setColor(Color.black);
g.drawRect(x,clientHeight, linelength*array[i], linethickness);
x+=15;
}
}
}

private class UpdateTextFieldThread extends SwingWorker<Void, Integer>
{
protected Void doInBackground()
{
display.repaint();
return null;
//should have repaint method in here somewhere

/*for (int i = 0; i < 50; i++) {
publish(i);
try {
Thread.sleep(THREAD_DELAY);
} catch (InterruptedException e) { }
}
return null;*/
}

// The parameter here is a list of all published data
// since the last call to process. We are interested
// in displaying only the latest one on the GUI.
protected void process(java.util.List<Integer> list)
{
// textfield.setText("" + list.get(list.size() - 1));
}
}

public static <T extends Comparable<T>> void insertionSort(int[] hue2)
{
for (int i = 1; i < hue2.length; i++) {
int thingToInsert = hue2[i];

int j = i - 1;
while (j >= 0 && thingToInsert<hue2[j]) {
hue2[j+1] = hue2[j];
j--;
}

hue2[j+1] = thingToInsert;
}
}

public static void main(String[]args) {
new project2();
}
}

最佳答案

masterPanel.add(display, BorderLayout.SOUTH);

您正在将显示面板添加到 BorderLayout 的南部。 SOUTH 约束将尊重组件的首选高度。您的组件的高度为 0,因此没有任何内容可显示。

每当您进行自定义绘制时,您还需要重写组件的 getPreferredSize() 方法,以提供组件的适当大小,以便布局管理器可以完成该工作。

关于java - super 油漆组件没有出现(我已经尝试了一切),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196530/

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