gpt4 book ai didi

java - JButtons 没有正确显示 borderlayout,还有向按钮添加 actionListener 的问题

转载 作者:行者123 更新时间:2023-11-29 05:22:01 26 4
gpt4 key购买 nike

我在这里遇到了 2 个关于相同代码的问题,首先,由于某种原因,JButtons 没有出现在当前配置的 GUI 上,但是代码运行了(调试器确实出现在该行代码中)。我似乎无法弄清楚问题出在哪里。

我遇到的第二个问题是 JButton.addActionListener(this) 不喜欢工作,我需要 actionListener 来触发不同类中的事件,所以我不能执行匿名 actionListener。

代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PPP extends JFrame{
static digit atp_hundred = new digit(); //Amount to pay
static digit atp_ten = new digit();
static digit atp_unit = new digit();
static digit atp_tenth = new digit();
static digit atp_hundreth = new digit();
static digit ltr_ten = new digit(); //Litres
static digit ltr_unit = new digit();
static digit ltr_tenth = new digit();
static digit ppl_hundred = new digit(); //Pence per litre
static digit ppl_ten = new digit();
static digit ppl_unit = new digit();
static digit ppl_tenth = new digit();
static PPP frame = new PPP();

public static void init() {
frame.setSize(1280,800);
frame.setLayout(new BorderLayout());
JPanel mainPanel = new mainPanel();
mainPanel.setLayout(new GridLayout(2,2));
frame.getContentPane().add(mainPanel);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});
frame.setVisible(true);
}

public static void main(String arg[]){
init();
while (true) {
for (int i = 0; i < 10; i++) {
atp_hundred.value = i;
atp_hundred.update();
frame.getContentPane().repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
frame.getContentPane().repaint();
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
}
}

public void refresh() {
repaint();
}

public PPP(){
super();
}

/*public void update(Graphics g) {
paint(g);
}*/

public static void atpCreate(Graphics g, int x, int y) {
atp_hundred.create(x, y);
drawPolygons(g, atp_hundred);
atp_ten.create(x+50,y);
drawPolygons(g, atp_ten);
atp_unit.create(x+100, y);
drawPolygons(g, atp_unit);
g.drawOval(x+145, y+60, 10,10);
atp_tenth.create(x+160, y);
drawPolygons(g, atp_tenth);
}

public static void ltrCreate(Graphics g, int x, int y) {
ltr_ten.create(x+50, y);
drawPolygons(g, ltr_ten);
ltr_unit.create(x+100, y);
drawPolygons(g, ltr_unit);
g.drawOval(x+145, y+60, 10, 10);
ltr_tenth.create(x+160, y);
drawPolygons(g, ltr_tenth);
}

public static void pplCreate(Graphics g, int x, int y) {
ppl_hundred.create(x, y);
drawPolygons(g, ppl_hundred);
ppl_ten.create(x+50,y);
drawPolygons(g, ppl_ten);
ppl_unit.create(x+100, y);
drawPolygons(g, ppl_unit);
g.drawOval(x+145, y+60, 10,10);
ppl_tenth.create(x+160, y);
drawPolygons(g, ppl_tenth);
}

public static void drawPolygons(Graphics g, digit d) {
if (d.top) {
g.setColor(Color.green);
g.fillPolygon(d.ptop);
} else {
g.setColor(Color.black);
g.drawPolygon(d.ptop);
}
if (d.topleft) {
g.setColor(Color.green);
g.fillPolygon(d.ptopleft);
} else {
g.setColor(Color.black);
g.drawPolygon(d.ptopleft);
}
if (d.topright) {
g.setColor(Color.green);
g.fillPolygon(d.ptopright);
} else {
g.setColor(Color.black);
g.drawPolygon(d.ptopright);
}
if (d.mid) {
g.setColor(Color.green);
g.fillPolygon(d.pmid);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pmid);
}
if (d.botleft) {
g.setColor(Color.green);
g.fillPolygon(d.pbotleft);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pbotleft);
}
if (d.botright) {
g.setColor(Color.green);
g.fillPolygon(d.pbotright);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pbotright);
}
if (d.bot) {
g.setColor(Color.green);
g.fillPolygon(d.pbot);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pbot);
}
g.setColor(Color.black);
}
}
class mainPanel extends JPanel {
public mainPanel() {
setPreferredSize(new Dimension(1280,800));
JPanel customerPanel = new customerPanel();
customerPanel.setLayout(new BorderLayout());
add(customerPanel);
//add(new customerButtons());
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
String title = "Pete's Petrol Pump Simulation vAlpha0.1";
g.setColor(Color.white);
//g.clearRect(0,0,640,500);
g.setColor(Color.black);
g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);
}
}
class customerPanel extends JPanel {
public customerPanel() {
//setPreferredSize(new Dimension(640,500));
//setPreferredSize(new Dimension(500,800));
customerButtons buttons = new customerButtons();
add(buttons, BorderLayout.SOUTH);
}

@Override
public void paintComponent(Graphics g){
//super.paintComponent(g);
/*g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
String title = "Pete's Petrol Pump Simulation vAlpha0.1";
g.setColor(Color.white);
g.clearRect(0,0,640,500);
g.setColor(Color.black);
g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);*/
g.drawRoundRect(20,100,600,250,5,5);
g.setFont(new Font("Gill Sans MT", Font.PLAIN, 20));
g.drawString("Amount to Pay: ", 40, 160);
PPP.atpCreate(g, 200, 110);
g.drawString("Litres Dispensed: ", 40, 240);
PPP.ltrCreate(g, 200, 190);
g.drawString("Pence per Litre: ", 40, 320);
PPP.pplCreate(g, 200, 270);
//System.out.println((g.getFontMetrics().getStringBounds("Amount to Pay", g).getWidth()));
}
}
class customerButtons extends JPanel {
public customerButtons() {
JButton remove = new JButton("Remove nozzle");
remove.setActionCommand("remove");
//System.out.println("Hello");
remove.addActionListener(this);
remove.setToolTipText("Replace the fucking nozzle");
JButton squeeze = new JButton("Squeeze nozzle");
squeeze.setActionCommand("squeeze");
squeeze.addActionListener(this);
squeeze.setToolTipText("Squeeze the fucking nozzle");
JButton stop = new JButton("Stop Squeeze");
stop.setActionCommand("stop");
stop.addActionListener(this);
stop.setToolTipText("Stop squeezing the fucking nozzle");
JButton replace = new JButton("Replace nozzle");
replace.setActionCommand("replace");
replace.addActionListener(this);
replace.setToolTipText("Replace the fucking nozzle");
add(remove);
add(squeeze);
add(stop);
add(replace);

}
}

class digit {
int value = 0;
boolean top = false;
boolean topleft = false;
boolean topright = false;
boolean mid = false;
boolean botleft = false;
boolean botright = false;
boolean bot = false;
public void update() {
switch(value) {
case 0: top = true;
topleft = true;
topright = true;
mid = false;
botleft = true;
botright = true;
bot = true;
break;
case 1: top = false;
topleft = false;
topright = true;
mid = false;
botleft = false;
botright = true;
bot = false;
break;
case 2: top = true;
topleft = false;
topright = true;
mid = true;
botleft = true;
botright = false;
bot = true;
break;
case 3: top = true;
topleft = false;
topright = true;
mid = true;
botleft = false;
botright = true;
bot = true;
break;
case 4: top = false;
topleft = true;
topright = true;
mid = true;
botleft = false;
botright = true;
bot = false;
break;
case 5: top = true;
topleft = true;
topright = false;
mid = true;
botleft = false;
botright = true;
bot = true;
break;
case 6: top = true;
topleft = true;
topright = false;
mid = true;
botleft = true;
botright = true;
bot = true;
break;
case 7: top = true;
topleft = false;
topright = true;
mid = false;
botleft = false;
botright = true;
bot = false;
break;
case 8: top = true;
topleft = true;
topright = true;
mid = true;
botleft = true;
botright = true;
bot = true;
break;
case 9: top = true;
topleft = true;
topright = true;
mid = true;
botleft = false;
botright = true;
bot = true;
break;
}
}
Polygon ptop = new Polygon();
Polygon ptopleft = new Polygon();
Polygon ptopright = new Polygon();
Polygon pmid = new Polygon();
Polygon pbotleft = new Polygon();
Polygon pbotright = new Polygon();
Polygon pbot = new Polygon();
public void create(int x, int y) {
ptop.addPoint(x+10, y);
ptop.addPoint(x+30, y);
ptop.addPoint(x+35, y+5);
ptop.addPoint(x+30, y+10);
ptop.addPoint(x+10, y+10);
ptop.addPoint(x+5, y+5);
ptopleft.addPoint(x+5, y+5);
ptopleft.addPoint(x+10, y+10);
ptopleft.addPoint(x+10, y+30);
ptopleft.addPoint(x+5, y+35);
ptopleft.addPoint(x, y+30);
ptopleft.addPoint(x, y+10);
ptopright.addPoint(x+35, y+5);
ptopright.addPoint(x+40, y+10);
ptopright.addPoint(x+40, y+30);
ptopright.addPoint(x+35, y+35);
ptopright.addPoint(x+30, y+30);
ptopright.addPoint(x+30, y+10);
pmid.addPoint(x+10, y+30);
pmid.addPoint(x+30, y+30);
pmid.addPoint(x+35, y+35);
pmid.addPoint(x+30, y+40);
pmid.addPoint(x+10, y+40);
pmid.addPoint(x+5, y+35);
pbotleft.addPoint(x+5, y+35);
pbotleft.addPoint(x+10, y+40);
pbotleft.addPoint(x+10, y+60);
pbotleft.addPoint(x+5, y+65);
pbotleft.addPoint(x, y+60);
pbotleft.addPoint(x, y+40);
pbotright.addPoint(x+35, y+35);
pbotright.addPoint(x+40, y+40);
pbotright.addPoint(x+40, y+60);
pbotright.addPoint(x+35, y+65);
pbotright.addPoint(x+30, y+60);
pbotright.addPoint(x+30, y+40);
pbot.addPoint(x+10, y+60);
pbot.addPoint(x+30, y+60);
pbot.addPoint(x+35, y+65);
pbot.addPoint(x+30, y+70);
pbot.addPoint(x+10, y+70);
pbot.addPoint(x+5, y+65);
}
}

感谢您的帮助。

最佳答案

  1. 您错误地使用了布局。在将布局设置到您的面板之前,您正在添加组件对于您的第一个问题,我的建议是阅读 LayoutManagers

  2. 第二个问题:您的 ActionListener 在哪里?查看修改后的代码。我已经添加了。

enter image description here

修改后的代码

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class PPP extends JFrame{
static digit atp_hundred = new digit(); //Amount to pay
static digit atp_ten = new digit();
static digit atp_unit = new digit();
static digit atp_tenth = new digit();
static digit atp_hundreth = new digit();
static digit ltr_ten = new digit(); //Litres
static digit ltr_unit = new digit();
static digit ltr_tenth = new digit();
static digit ppl_hundred = new digit(); //Pence per litre
static digit ppl_ten = new digit();
static digit ppl_unit = new digit();
static digit ppl_tenth = new digit();
static PPP frame = new PPP();

public static void init() {
frame.setSize(1280,800);
frame.setLayout(new BorderLayout());
JPanel mainPanel = new mainPanel();
mainPanel.setLayout(new GridLayout(2,2));
frame.getContentPane().add(mainPanel);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});
frame.setVisible(true);
}

public static void main(String arg[]){
init();
while (true) {
for (int i = 0; i < 10; i++) {
atp_hundred.value = i;
atp_hundred.update();
frame.getContentPane().repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
frame.getContentPane().repaint();
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
}
}

public void refresh() {
repaint();
}

public PPP(){
super();
}

/*public void update(Graphics g) {
paint(g);
}*/

public static void atpCreate(Graphics g, int x, int y) {
atp_hundred.create(x, y);
drawPolygons(g, atp_hundred);
atp_ten.create(x+50,y);
drawPolygons(g, atp_ten);
atp_unit.create(x+100, y);
drawPolygons(g, atp_unit);
g.drawOval(x+145, y+60, 10,10);
atp_tenth.create(x+160, y);
drawPolygons(g, atp_tenth);
}

public static void ltrCreate(Graphics g, int x, int y) {
ltr_ten.create(x+50, y);
drawPolygons(g, ltr_ten);
ltr_unit.create(x+100, y);
drawPolygons(g, ltr_unit);
g.drawOval(x+145, y+60, 10, 10);
ltr_tenth.create(x+160, y);
drawPolygons(g, ltr_tenth);
}

public static void pplCreate(Graphics g, int x, int y) {
ppl_hundred.create(x, y);
drawPolygons(g, ppl_hundred);
ppl_ten.create(x+50,y);
drawPolygons(g, ppl_ten);
ppl_unit.create(x+100, y);
drawPolygons(g, ppl_unit);
g.drawOval(x+145, y+60, 10,10);
ppl_tenth.create(x+160, y);
drawPolygons(g, ppl_tenth);
}

public static void drawPolygons(Graphics g, digit d) {
if (d.top) {
g.setColor(Color.green);
g.fillPolygon(d.ptop);
} else {
g.setColor(Color.black);
g.drawPolygon(d.ptop);
}
if (d.topleft) {
g.setColor(Color.green);
g.fillPolygon(d.ptopleft);
} else {
g.setColor(Color.black);
g.drawPolygon(d.ptopleft);
}
if (d.topright) {
g.setColor(Color.green);
g.fillPolygon(d.ptopright);
} else {
g.setColor(Color.black);
g.drawPolygon(d.ptopright);
}
if (d.mid) {
g.setColor(Color.green);
g.fillPolygon(d.pmid);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pmid);
}
if (d.botleft) {
g.setColor(Color.green);
g.fillPolygon(d.pbotleft);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pbotleft);
}
if (d.botright) {
g.setColor(Color.green);
g.fillPolygon(d.pbotright);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pbotright);
}
if (d.bot) {
g.setColor(Color.green);
g.fillPolygon(d.pbot);
} else {
g.setColor(Color.black);
g.drawPolygon(d.pbot);
}
g.setColor(Color.black);
}
}
class mainPanel extends JPanel {
public mainPanel() {
setPreferredSize(new Dimension(1280,800));
JPanel customerPanel = new customerPanel();

add(customerPanel);
//add(new customerButtons());
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
String title = "Pete's Petrol Pump Simulation vAlpha0.1";
g.setColor(Color.white);
//g.clearRect(0,0,640,500);
g.setColor(Color.black);
g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);
}
}
class customerPanel extends JPanel {
public customerPanel() {
//setPreferredSize(new Dimension(640,500));
//setPreferredSize(new Dimension(500,800));
setLayout(new BorderLayout());
customerButtons buttons = new customerButtons();
add(buttons, BorderLayout.SOUTH);
}

@Override
public void paintComponent(Graphics g){
//super.paintComponent(g);
/*g.setFont(new Font("Copperplate Gothic Light", Font.PLAIN, 30));
String title = "Pete's Petrol Pump Simulation vAlpha0.1";
g.setColor(Color.white);
g.clearRect(0,0,640,500);
g.setColor(Color.black);
g.drawString(title, (int)(640 - (g.getFontMetrics().getStringBounds(title, g).getWidth() / 2)), 75);*/
g.drawRoundRect(20,100,600,250,5,5);
g.setFont(new Font("Gill Sans MT", Font.PLAIN, 20));
g.drawString("Amount to Pay: ", 40, 160);
PPP.atpCreate(g, 200, 110);
g.drawString("Litres Dispensed: ", 40, 240);
PPP.ltrCreate(g, 200, 190);
g.drawString("Pence per Litre: ", 40, 320);
PPP.pplCreate(g, 200, 270);
//System.out.println((g.getFontMetrics().getStringBounds("Amount to Pay", g).getWidth()));
}
}
class customerButtons extends JPanel implements ActionListener {
public customerButtons() {
JButton remove = new JButton("Remove nozzle");
remove.setActionCommand("remove");
//System.out.println("Hello");
remove.addActionListener(this);
remove.setToolTipText("Replace the fucking nozzle");
JButton squeeze = new JButton("Squeeze nozzle");
squeeze.setActionCommand("squeeze");
squeeze.addActionListener(this);
squeeze.setToolTipText("Squeeze the fucking nozzle");
JButton stop = new JButton("Stop Squeeze");
stop.setActionCommand("stop");
stop.addActionListener(this);
stop.setToolTipText("Stop squeezing the fucking nozzle");
JButton replace = new JButton("Replace nozzle");
replace.setActionCommand("replace");
replace.addActionListener(this);
replace.setToolTipText("Replace the fucking nozzle");
add(remove);
add(squeeze);
add(stop);
add(replace);

}

public void actionPerformed(ActionEvent e) {
// do your stuff here

}
}

class digit {
int value = 0;
boolean top = false;
boolean topleft = false;
boolean topright = false;
boolean mid = false;
boolean botleft = false;
boolean botright = false;
boolean bot = false;
public void update() {
switch(value) {
case 0: top = true;
topleft = true;
topright = true;
mid = false;
botleft = true;
botright = true;
bot = true;
break;
case 1: top = false;
topleft = false;
topright = true;
mid = false;
botleft = false;
botright = true;
bot = false;
break;
case 2: top = true;
topleft = false;
topright = true;
mid = true;
botleft = true;
botright = false;
bot = true;
break;
case 3: top = true;
topleft = false;
topright = true;
mid = true;
botleft = false;
botright = true;
bot = true;
break;
case 4: top = false;
topleft = true;
topright = true;
mid = true;
botleft = false;
botright = true;
bot = false;
break;
case 5: top = true;
topleft = true;
topright = false;
mid = true;
botleft = false;
botright = true;
bot = true;
break;
case 6: top = true;
topleft = true;
topright = false;
mid = true;
botleft = true;
botright = true;
bot = true;
break;
case 7: top = true;
topleft = false;
topright = true;
mid = false;
botleft = false;
botright = true;
bot = false;
break;
case 8: top = true;
topleft = true;
topright = true;
mid = true;
botleft = true;
botright = true;
bot = true;
break;
case 9: top = true;
topleft = true;
topright = true;
mid = true;
botleft = false;
botright = true;
bot = true;
break;
}
}
Polygon ptop = new Polygon();
Polygon ptopleft = new Polygon();
Polygon ptopright = new Polygon();
Polygon pmid = new Polygon();
Polygon pbotleft = new Polygon();
Polygon pbotright = new Polygon();
Polygon pbot = new Polygon();
public void create(int x, int y) {
ptop.addPoint(x+10, y);
ptop.addPoint(x+30, y);
ptop.addPoint(x+35, y+5);
ptop.addPoint(x+30, y+10);
ptop.addPoint(x+10, y+10);
ptop.addPoint(x+5, y+5);
ptopleft.addPoint(x+5, y+5);
ptopleft.addPoint(x+10, y+10);
ptopleft.addPoint(x+10, y+30);
ptopleft.addPoint(x+5, y+35);
ptopleft.addPoint(x, y+30);
ptopleft.addPoint(x, y+10);
ptopright.addPoint(x+35, y+5);
ptopright.addPoint(x+40, y+10);
ptopright.addPoint(x+40, y+30);
ptopright.addPoint(x+35, y+35);
ptopright.addPoint(x+30, y+30);
ptopright.addPoint(x+30, y+10);
pmid.addPoint(x+10, y+30);
pmid.addPoint(x+30, y+30);
pmid.addPoint(x+35, y+35);
pmid.addPoint(x+30, y+40);
pmid.addPoint(x+10, y+40);
pmid.addPoint(x+5, y+35);
pbotleft.addPoint(x+5, y+35);
pbotleft.addPoint(x+10, y+40);
pbotleft.addPoint(x+10, y+60);
pbotleft.addPoint(x+5, y+65);
pbotleft.addPoint(x, y+60);
pbotleft.addPoint(x, y+40);
pbotright.addPoint(x+35, y+35);
pbotright.addPoint(x+40, y+40);
pbotright.addPoint(x+40, y+60);
pbotright.addPoint(x+35, y+65);
pbotright.addPoint(x+30, y+60);
pbotright.addPoint(x+30, y+40);
pbot.addPoint(x+10, y+60);
pbot.addPoint(x+30, y+60);
pbot.addPoint(x+35, y+65);
pbot.addPoint(x+30, y+70);
pbot.addPoint(x+10, y+70);
pbot.addPoint(x+5, y+65);
}
}

但在这种情况下,请尝试创建如下布局:

enter image description here

关于java - JButtons 没有正确显示 borderlayout,还有向按钮添加 actionListener 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325790/

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