​https://gitee.com/dgwcode/MyJavaCode​

Swing 实现的Gui链表_i++

 

-Freight类可以实现的功能
·构造方法:初始空车厢有5个
·装货:当运往某地点的货物大于等于4个的时候,如果有空车箱,则先占用整个空车厢,如果没有空车厢,则在尾部新增一个车厢节点,存放货物(假设不存在超过7个的情况),并且从链表头部依次搜索是否有车厢还有空位,如果有,将多出来的零散货物分别装进车厢(如上图的5件B,先装满第二个车厢,然后搜索后再将剩余的一件装入第一车厢)
·卸货:当到达某地时,以B为例,搜索链表,如果某个车厢都是该地货物,则从链表中删除此节点,并将其他零散B地货物从车厢中去除。

java Swing 实现的链表类

Swing 实现的Gui链表_java_02

1 package M;
2
3 import java.awt.BorderLayout;
4 import java.awt.Container;
5 import java.awt.Dimension;
6 import java.awt.FlowLayout;
7 import java.awt.Font;
8
9 import javax.swing.JButton;
10 import javax.swing.JFrame;
11 import javax.swing.JOptionPane;
12 import javax.swing.JPanel;
13 import javax.swing.JScrollPane;
14 import javax.swing.JSplitPane;
15 import javax.swing.JTextArea;
16
17 class Carriage { /// 车厢
18 int size = 0;// 表示当前车厢已经装了几个活物
19 char a[] = new char[4];/// a用来装货物
20 Carriage next;/// 连接下一个
21 }
22 public class A {
23
24 private static int FLAG = -1;
25 private final static int WIDTH = 800;
26 private final static int HEIGHT = 400;
27 private static Container container;
28 private static JPanel UpPanel;
29 private static JPanel bottomPanel;
30 private static JButton Input0;
31 private static JButton Input1;
32 private static JButton Input2;
33 private static JTextArea viewArea;
34 private static JScrollPane jspane1;
35 private static JSplitPane jsp;
36 static int size = 5;/// 最初的5个车厢
37 static Carriage Head = new Carriage();/// 最开始的都头结点
38
39 public static void init() {
40 // TODO Auto-generated constructor stub
41 Carriage p = Head;
42 for (int i = 0; i < 5; i++) {
43 Carriage r = new Carriage();
44 p.next = r;//// 连接
45 p = r;
46 }
47 }
48
49 public static void main(String[] args) {
50 init();
51 InitFrame();
52
53 }
54
55 public static void Logic(int a, char car, int num) {
56 if (a == 0) {
57 //System.out.println("准备打印链表状态");
58 viewArea.append("准备打印链表状态\n");
59 for (int i = 0; i < size; i++) {
60 PrintCarUp();
61 //System.out.print(" ");
62 viewArea.append(" ");
63 }
64 //System.out.println();
65 viewArea.append("\n");
66 Carriage p = Head.next;
67 while (p != null) {
68 //System.out.print("*");
69 viewArea.append("*");
70 for (int i = 0; i < 4; i++) {
71 if (p.a[i] == 0)
72 //System.out.print(" ");
73 viewArea.append(" ");
74 else
75 //System.out.print(p.a[i]);
76 viewArea.append(String.valueOf(p.a[i]));
77 }
78 //System.out.print("*");
79 viewArea.append("*");
80 showNext();
81 p = p.next;
82 }
83 //System.out.println();
84 viewArea.append("\n");
85 for (int i = 0; i < size; i++) {
86 PrintCarUp();
87 //System.out.print(" ");
88 viewArea.append(" ");
89 }
90 //System.out.println();
91 viewArea.append("\n");
92 } else if (a == 1) {
93 //System.out.println("请输入两个数,第一个为字母,第二个数字,例如 A 2");
94 viewArea.append("请输入两个数,第一个为字母,第二个数字,例如 A 2");
95 while (num != 0) {
96 Carriage p = Head;
97 while (p.next != null) {
98 if (p.next.size == 4)
99 p = p.next;
100 for (int i = 0; i < 4; i++) {
101 if (p.next.a[i] == 0) {
102 p.next.a[i] = car;
103 p.next.size++;
104 num--;
105 }
106 if (num == 0)
107 break;
108 }
109 if (num == 0)
110 break;
111 p = p.next;
112 }
113 while (num != 0) {
114 Carriage r = new Carriage();
115 p.next = r;
116 p = r;
117 for (int i = 0; i < 4 && num != 0; i++) {
118 r.a[i] = car;
119 num--;
120 }
121 }
122 }
123 } else {
124 //System.out.println("请输入一个字母,例如 A");
125 viewArea.append("请输入一个字母,例如 A");
126 Carriage p = Head;
127 int num1 = 0;
128 while (p.next != null) {
129 for (int i = 0; i < 4; i++) {
130 if (p.next.a[i] == car) {
131 p.next.a[i] = 0;
132 num1++;
133 }
134 }
135 if (num1 == 4) {
136 Carriage r = p.next;
137 p.next = r.next;
138 r.next = null;/// 回收利用
139 size--;
140 } else {
141 p = p.next;
142 }
143 num1 = 0;
144 }
145 }
146 }
147
148 private static void InitFrame() {
149 JFrame Main = new JFrame("Carriage");
150 container = Main.getContentPane();
151 container.setLayout(new BorderLayout());
152 Main.setSize(WIDTH, HEIGHT);
153 Main.setLocationRelativeTo(null);
154 Main.setLayout(new BorderLayout());
155 Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
156
157 Input0 = new JButton("打印链表状态0");
158 Input1 = new JButton("装货1");
159 Input2 = new JButton("卸货2");
160 UpPanel = new JPanel(new FlowLayout());
161 bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
162 viewArea = new JTextArea("选择按钮 0,1,2,输入-1退出");
163 viewArea.setLineWrap(true);
164 viewArea.setFont(new Font("黑体", Font.BOLD, 15));
165 jspane1 = new JScrollPane(viewArea) {
166 private static final long serialVersionUID = 1L;
167
168 @Override
169 public Dimension getPreferredSize() {
170 return new Dimension(780, 280);
171 }
172
173 @Override
174 public Font getFont() {
175 return new Font("黑体", Font.BOLD, 12);
176 }
177 };
178
179 UpPanel.setSize(WIDTH, 150);
180 bottomPanel.setSize(WIDTH, 250);
181
182 UpPanel.add(Input0);
183 UpPanel.add(Input1);
184 UpPanel.add(Input2);
185
186 bottomPanel.add(jspane1);
187
188 jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, UpPanel, bottomPanel);
189 jsp.setDividerLocation(70); // 设置拆分窗格分频器初始位置
190 jsp.setDividerSize(1); // 设置分频器大小
191 container.add(jsp);
192 // button lisenner
193 Input0.addActionListener(e -> {
194 FLAG=0;
195 Logic(FLAG, '0', 0);
196 });
197 Input1.addActionListener(e -> {
198 String inputValue = JOptionPane.showInputDialog("请输入两个数,第一个为字母,第二个数字,例如 A 2(注意空格)");
199 // System.out.println("请输入两个数,第一个为字母,第二个数字,例如 A 2");
200 if (inputValue.equals(" ") || inputValue == null) {
201
202 } else {
203 char car = inputValue.charAt(0);
204 int num = inputValue.charAt(2) - '0';
205 FLAG=1;
206 Logic(FLAG, car, num);
207 // System.out.println(car+" "+num);
208 }
209 });
210 Input2.addActionListener(e -> {
211 String inputValue = JOptionPane.showInputDialog("请输入一个字母,例如 A");
212 if (inputValue.equals(" ") || inputValue == null) {
213
214 } else {
215 char car = inputValue.charAt(0);
216 FLAG=2;
217 Logic(FLAG, car, 0);
218 // System.out.println(car+" "+num);
219 }
220
221 });
222
223 Main.setVisible(true);
224 }
225
226 private static void PrintCarUp() {
227 //System.out.print("******");
228 viewArea.append("******");
229 }
230
231 private static void PrintCarleft() {
232 //System.out.print("*");
233 viewArea.append("*");
234 }
235
236 private static void PrintCarright() {
237 //System.out.print("*");
238 viewArea.append("*");
239 }
240
241 private static void PrintCardown() {
242 //System.out.print("******");
243 viewArea.append("******");
244 }
245
246 private static void showNext() {
247 //System.out.print("--->");
248 viewArea.append("--->");
249 }
250
251 private static void print() {
252 //System.out.println("请输入0 1 2 或者-1退出");
253 viewArea.append("请输入0 1 2 或者-1退出");
254 show();
255 }
256 public static void show() {
257 //System.out.println("0-打印链表状态");
258 //System.out.println("1-装货");
259 //System.out.println("2-卸货");
260 viewArea.append("0-打印链表状态");
261 viewArea.append("1-装货");
262 viewArea.append("2-卸货");
263 }
264