gpt4 book ai didi

java - 基于组件对 JPanel 进行排序

转载 作者:行者123 更新时间:2023-11-30 08:08:50 25 4
gpt4 key购买 nike

我的程序生成从 0 到 12 的随机数,但如果结果是 12,它会将破折号设置为 JLabel 的文本,而不是生成的数字。

现在,我想根据 JLabel 内容对我的 JPanel 进行升序排序。如果数字相似,黑色的 JPanels 放在左边。它工作正常,除非包含破折号,其中它没有正确排序。我想在任何地方插入包含破折号的 JPanels,但它没有按预期工作。

我的程序的较短版本的屏幕截图:

纯数字:

enter image description here

包含破折号:

enter image description here

这是我的代码的较短版本(使用整数排序逻辑):

import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Comparator;

public class SortFrames extends JFrame
{
static ArrayList<JPanel> panels = new ArrayList<JPanel>();
JPanel panel = new JPanel();
JPanel sortPane = new JPanel();
int toWrite = 0;
int colorGen = 0;
int comparison = 0;
Random rand = new Random();

public SortFrames()
{
for(int i = 0; i<4;i++)
{
panels.add(new JPanel());
}

for(JPanel p: panels)
{
toWrite = rand.nextInt(13);
colorGen = rand.nextInt(2);
p.add(new JLabel());
JLabel lblToSet = (JLabel)p.getComponent(0);

if(colorGen == 0)
{
p.setBackground(Color.BLACK);
lblToSet.setForeground(Color.WHITE);
}

if(colorGen == 1)
{
p.setBackground(Color.WHITE);
lblToSet.setForeground(Color.BLACK);
}

if(toWrite != 12){lblToSet.setText("" +toWrite);}
if(toWrite == 12){lblToSet.setText("-");}
p.setPreferredSize(new Dimension(30, 30));
panel.add(p);
}


sortMethod();

for(JPanel p: panels)
{
panel.add(p);
panel.revalidate();
}

add(panel);
panel.setPreferredSize(new Dimension(300, 300));
setPreferredSize(new Dimension(300, 300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
setLocationRelativeTo(null);
}

public void sortMethod()
{
for(int i = 0; i<(panels.size());i++)
{
for(int j = i+1; j<(panels.size());j++)
{
JLabel one = (JLabel)(panels.get(i)).getComponent(0);
JLabel two = (JLabel)(panels.get(j)).getComponent(0);
String lblOne = one.getText();
String lblTwo = two.getText();

if(!lblOne.equals("-") && !lblTwo.equals("-"))
{
int comp1 = Integer.parseInt(lblOne);
int comp2 = Integer.parseInt(lblTwo);
JPanel pnl1 = panels.get(i);
JPanel pnl2 = panels.get(j);

if(comp1 == comp2)
{
if(pnl1.getBackground() == Color.BLACK && pnl2.getBackground() == Color.WHITE)
{
panels.set(i, pnl1);
panels.set(j, pnl2);
}

if(pnl1.getBackground() == Color.WHITE && pnl2.getBackground() == Color.BLACK)
{
panels.set(i, pnl2);
panels.set(j, pnl1);
}
}

if(comp1 != comp2)
{
if(comp1>comp2)
{
panels.set(i, pnl2);
panels.set(j, pnl1);
}
}
}

if(lblOne.equals("-") && !lblTwo.equals("-"))
{
JPanel pnl1 = panels.get(i);
panels.set(rand.nextInt(panels.size()), pnl1);
}

if(!lblOne.equals("-") && lblTwo.equals("-"))
{
JPanel pnl2 = panels.get(j);
panels.set(rand.nextInt(panels.size()), pnl2);
}
}
}
}

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

我还有另一种方法,即使用 Comparator 类,它也会产生同样的问题(这会根据前景对相等的数字进行排序,但仍然与根据背景对相等的数字进行排序相同,因此它对上述问题没有影响)。

private static class JPanelSort implements Comparator<JPanel>
{
@Override
public int compare(JPanel arg0, JPanel arg1)
{
JLabel one = ((JLabel) arg0.getComponent(0));
JLabel two = ((JLabel) arg1.getComponent(0));
String firstContent = one.getText();
String secondContent = two.getText();

try
{
comparisonRes = Integer.compare(Integer.parseInt(firstContent), Integer.parseInt(secondContent));
if(comparisonRes == 0)
{
if(one.getForeground() == Color.BLACK && two.getForeground() == Color.WHITE)
{
comparisonRes = 1;
}
if(two.getForeground() == Color.BLACK && one.getForeground() == Color.WHITE)
{
comparisonRes = -1;
}
}
}

catch(NumberFormatException e)
{
comparisonRes = 0;
}

return comparisonRes;
}
}

请告诉我你的想法。谢谢。

最佳答案

对数据进行排序比对 JPanel 进行排序要容易得多。

这是显示您的数字的 mu GUI。

Sort Frames GUI

因此,让我们创建一个 Java 对象来保存卡片数据。

public class DataModel {
private final int number;
private final int colorNumber;

private final Color backgroundColor;
private final Color foregroundColor;

public DataModel(int number, int colorNumber, Color backgroundColor,
Color foregroundColor) {
this.number = number;
this.colorNumber = colorNumber;
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
}

public int getNumber() {
return number;
}

public int getColorNumber() {
return colorNumber;
}

public Color getBackgroundColor() {
return backgroundColor;
}

public Color getForegroundColor() {
return foregroundColor;
}

}

非常简单。我们有用于保存信息的字段和用于检索信息的 getter 。我们可以将所有字段设置为最终字段,因为一旦我们设置了值,我们就不会更改任何内容。

排序类也很简单。

public class DataModelComparator implements Comparator<DataModel> {

@Override
public int compare(DataModel o1, DataModel o2) {
if (o1.getNumber() < o2.getNumber()) {
return -1;
} else if (o1.getNumber() > o2.getNumber()) {
return 1;
} else {
if (o1.getColorNumber() < o2.getColorNumber()) {
return -1;
} else if (o1.getColorNumber() > o2.getColorNumber()) {
return 1;
} else {
return 0;
}
}
}

}

由于我们保留了颜色编号,因此按颜色排序就像排序数字一样简单。

现在我们已经将数据移动到它自己的列表中,我们可以专注于创建 GUI。

package com.ggl.testing;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SortFrames implements Runnable {
private List<DataModel> dataModels;

private JPanel[] panels;

private JLabel[] labels;

private Random random = new Random();

public SortFrames() {
this.dataModels = new ArrayList<>();
this.random = new Random();

for (int i = 0; i < 4; i++) {
int number = random.nextInt(13);
int colorNumber = random.nextInt(2);
Color backgroundColor = Color.BLACK;
Color foregroundColor = Color.WHITE;
if (colorNumber == 1) {
backgroundColor = Color.WHITE;
foregroundColor = Color.BLACK;
}
dataModels.add(new DataModel(number, colorNumber, backgroundColor,
foregroundColor));
}

Collections.sort(dataModels, new DataModelComparator());
}

@Override
public void run() {
JFrame frame = new JFrame("Sort Frames");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainPanel = new JPanel();

panels = new JPanel[dataModels.size()];
labels = new JLabel[dataModels.size()];

for (int i = 0; i < dataModels.size(); i++) {
DataModel dataModel = dataModels.get(i);
panels[i] = new JPanel();
panels[i].setBackground(dataModel.getBackgroundColor());

labels[i] = new JLabel(getDisplayText(dataModel));
labels[i].setBackground(dataModel.getBackgroundColor());
labels[i].setForeground(dataModel.getForegroundColor());

panels[i].add(labels[i]);
mainPanel.add(panels[i]);
}

frame.add(mainPanel);

frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}

private String getDisplayText(DataModel dataModel) {
if (dataModel.getNumber() == 12) {
return "-";
} else {
return Integer.toString(dataModel.getNumber());
}
}

public static void main(String args[]) {
SwingUtilities.invokeLater(new SortFrames());
}

public class DataModel {
private final int number;
private final int colorNumber;

private final Color backgroundColor;
private final Color foregroundColor;

public DataModel(int number, int colorNumber, Color backgroundColor,
Color foregroundColor) {
this.number = number;
this.colorNumber = colorNumber;
this.backgroundColor = backgroundColor;
this.foregroundColor = foregroundColor;
}

public int getNumber() {
return number;
}

public int getColorNumber() {
return colorNumber;
}

public Color getBackgroundColor() {
return backgroundColor;
}

public Color getForegroundColor() {
return foregroundColor;
}

}

public class DataModelComparator implements Comparator<DataModel> {

@Override
public int compare(DataModel o1, DataModel o2) {
if (o1.getNumber() < o2.getNumber()) {
return -1;
} else if (o1.getNumber() > o2.getNumber()) {
return 1;
} else {
if (o1.getColorNumber() < o2.getColorNumber()) {
return -1;
} else if (o1.getColorNumber() > o2.getColorNumber()) {
return 1;
} else {
return 0;
}
}
}

}
}

这里要吸取的教训是:

  1. 将数据与 View 分开。
  2. 一次只关注问题的一部分。分而治之。

关于java - 基于组件对 JPanel 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32904518/

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