英文:
How to display an ArrayList using Swing?
问题
// 你有两个包含数据的 `ArrayList` 实例,希望使用 Swing 将数据显示为框或其数据。
// 最终目标是将这两个数组列表显示为交汇点和道路的矩阵。
// 你有一个包含两者的对象:
Map map = new Map(10);
System.out.println(map.calcShortestPath(map.getJunctions().get(4), map.getJunctions().get(0)));
System.out.println("\n Map #2");
ArrayList<Junction> junctions = new ArrayList<Junction>();
junctions.add(new Junction(0, 0));
junctions.add(new Junction(0, 3));
junctions.add(new Junction(4, 3));
junctions.add(new Junction(4, 0));
ArrayList<Road> roads = new ArrayList<Road>();
roads.add(new Road(junctions.get(0), junctions.get(1)));
roads.add(new Road(junctions.get(1), junctions.get(2)));
roads.add(new Road(junctions.get(2), junctions.get(3)));
roads.add(new Road(junctions.get(3), junctions.get(0)));
roads.add(new Road(junctions.get(0), junctions.get(2)));
map = new Map(junctions, roads);
// 我尝试过的方法:
// - 使用 `JTable` - 对此似乎不是正确的选择。
// - 使用 `JList` - 似乎不起作用,因为我尝试使用以下代码查看其中一个列表:
JList<Junction> displayList = new JList<>(junctions.toArray(new Junction[0]));
JScrollPane scrollPane = new JScrollPane(displayList);
getContentPane().add(scrollPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
// 我认为使用 `JList`,我离目标更近了,但我认为我可能没有以正确的方式进行操作。
英文:
I have two ArrayList
instances that contain some data and I want that data to be displayed as boxes or their data by using Swing.
The end goal is to display both array lists as one matrix of junctions and roads.
I have an object that contains both:
Map map = new Map(10);
System.out.println(map.calcShortestPath(map.getJunctions().get(4), map.getJunctions().get(0)));
System.out.println("\n Map #2");
ArrayList<Junction> junctions = new ArrayList<Junction>();
junctions.add(new Junction(0, 0));
junctions.add(new Junction(0, 3));
junctions.add(new Junction(4, 3));
junctions.add(new Junction(4, 0));
ArrayList<Road> roads = new ArrayList<Road>();
roads.add(new Road(junctions.get(0), junctions.get(1)));
roads.add(new Road(junctions.get(1), junctions.get(2)));
roads.add(new Road(junctions.get(2), junctions.get(3)));
roads.add(new Road(junctions.get(3), junctions.get(0)));
roads.add(new Road(junctions.get(0), junctions.get(2)));
map = new Map(junctions, roads);
The things I have tried so far:
-
Using
JTable
- didn't seem to be the correct choice for this. -
Using
JList
- didn't seem to work as I tried to see one of the lists with this
code:JList<Juncion> displayList = new JList<>(junctions.toArray(new String[0])); JScrollPane scrollPane = new JScrollPane(displayList); getContentPane().add(scrollPane); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true);
I think I'm getting closer to this by using the JList
but I think I'm not doing this the correct way.
答案1
得分: 2
我认为制作一个图形显示示例可能会很有趣。
以下是我创建的GUI界面。
交叉点由方块表示,道路由线条表示。
我首先创建了一个名为Graph
的类,用于保存一个交叉点列表和一个道路列表。我不得不猜测Junction
类中的数字代表什么。我假设它们是X和Y坐标。
创建了Graph
类(模型类)后,编写绘图面板和paintComponent
方法就很直接了。
以下是代码。您需要进行修改才能显示多个图形。
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GraphDisplay implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GraphDisplay());
}
private DrawingPanel drawingPanel;
private Graph graph;
private JFrame frame;
public GraphDisplay() {
this.graph = new Graph();
}
@Override
public void run() {
frame = new JFrame("Graph Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawingPanel = new DrawingPanel(graph);
frame.add(drawingPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Graph graph;
private Tuple xTuple;
private Tuple yTuple;
public DrawingPanel(Graph graph) {
this.graph = graph;
this.xTuple = graph.getXRange();
this.yTuple = graph.getYRange();
this.setBackground(Color.WHITE);
this.setPreferredSize(new Dimension(400, 400));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xSpacing = getWidth() / (xTuple.getMaximum() -
xTuple.getMinimum() + 2);
int ySpacing = getHeight() / (yTuple.getMaximum() -
yTuple.getMinimum() + 2);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(5f));
Font font = getFont().deriveFont(16f);
g2d.setFont(font);
List<Junction> junctions = graph.getJunctions();
for (int i = 0; i < junctions.size(); i++) {
Junction junction = junctions.get(i);
int x = (junction.getX() + 1) * xSpacing;
int y = (junction.getY() + 1) * ySpacing;
g.drawRect(x - 16, y - 16, 32, 32);
}
List<Road> roads = graph.getRoads();
for (int i = 0; i < roads.size(); i++) {
Road road = roads.get(i);
Junction origin = road.getOrigin();
Junction destination = road.getDestination();
int x1 = (origin.getX() + 1) * xSpacing;
int y1 = (origin.getY() + 1) * ySpacing;
int x2 = (destination.getX() + 1) * xSpacing;
int y2 = (destination.getY() + 1) * ySpacing;
g2d.drawLine(x1, y1, x2, y2);
}
}
}
// Other classes (Graph, Junction, Road, Tuple) omitted for brevity.
}
英文:
I thought it might be interesting to whip up an example of a graph display.
Here's the GUI I created.
The junctions are represented by squares, and the roads are represented by lines.
The first thing I did was create a Graph
class to hold a List
of junctions and a List
of roads. I had to guess what the numbers of the Junction
class represented. I assumed they were X and Y coordinates.
Once I created the Graph
class (model class), writing the drawing panel and the paintComponent
method was straightforward.
Here's the code. You would need to modify it to display more than one graph.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GraphDisplay implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new GraphDisplay());
}
private DrawingPanel drawingPanel;
private Graph graph;
private JFrame frame;
public GraphDisplay() {
this.graph = new Graph();
}
@Override
public void run() {
frame = new JFrame("Graph Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawingPanel = new DrawingPanel(graph);
frame.add(drawingPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private Graph graph;
private Tuple xTuple;
private Tuple yTuple;
public DrawingPanel(Graph graph) {
this.graph = graph;
this.xTuple = graph.getXRange();
this.yTuple = graph.getYRange();
this.setBackground(Color.WHITE);
this.setPreferredSize(new Dimension(400, 400));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xSpacing = getWidth() / (xTuple.getMaximum() -
xTuple.getMinimum() + 2);
int ySpacing = getHeight() / (yTuple.getMaximum() -
yTuple.getMinimum() + 2);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(5f));
Font font = getFont().deriveFont(16f);
g2d.setFont(font);
List<Junction> junctions = graph.getJunctions();
for (int i = 0; i < junctions.size(); i++) {
Junction junction = junctions.get(i);
int x = (junction.getX() + 1) * xSpacing;
int y = (junction.getY() + 1) * ySpacing;
g.drawRect(x - 16, y - 16, 32, 32);
}
List<Road> roads = graph.getRoads();
for (int i = 0; i < roads.size(); i++) {
Road road = roads.get(i);
Junction origin = road.getOrigin();
Junction destination = road.getDestination();
int x1 = (origin.getX() + 1) * xSpacing;
int y1 = (origin.getY() + 1) * ySpacing;
int x2 = (destination.getX() + 1) * xSpacing;
int y2 = (destination.getY() + 1) * ySpacing;
g2d.drawLine(x1, y1, x2, y2);
}
}
}
public class Graph {
private final List<Junction> junctions;
private final List<Road> roads;
public Graph() {
junctions = new ArrayList<Junction>();
junctions.add(new Junction(0, 0));
junctions.add(new Junction(0, 3));
junctions.add(new Junction(4, 3));
junctions.add(new Junction(4, 0));
roads = new ArrayList<Road>();
roads.add(new Road(junctions.get(0), junctions.get(1)));
roads.add(new Road(junctions.get(1), junctions.get(2)));
roads.add(new Road(junctions.get(2), junctions.get(3)));
roads.add(new Road(junctions.get(3), junctions.get(0)));
roads.add(new Road(junctions.get(0), junctions.get(2)));
}
public List<Junction> getJunctions() {
return junctions;
}
public List<Road> getRoads() {
return roads;
}
public Tuple getXRange() {
int minimum = junctions.get(0).getX();
int maximum = minimum;
for (int i = 1; i < junctions.size(); i++) {
int x = junctions.get(i).getX();
minimum = Math.min(minimum, x);
maximum = Math.max(maximum, x);
}
return new Tuple(minimum, maximum);
}
public Tuple getYRange() {
int minimum = junctions.get(0).getY();
int maximum = minimum;
for (int i = 1; i < junctions.size(); i++) {
int y = junctions.get(i).getY();
minimum = Math.min(minimum, y);
maximum = Math.max(maximum, y);
}
return new Tuple(minimum, maximum);
}
}
public class Road {
private final Junction origin;
private final Junction destination;
public Road(Junction origin, Junction destination) {
this.origin = origin;
this.destination = destination;
}
public Junction getOrigin() {
return origin;
}
public Junction getDestination() {
return destination;
}
}
public class Junction {
private final int x;
private final int y;
public Junction(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Tuple {
private final int minimum;
private final int maximum;
public Tuple(int minimum, int maximum) {
this.minimum = minimum;
this.maximum = maximum;
}
public int getMinimum() {
return minimum;
}
public int getMaximum() {
return maximum;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论