gpt4 book ai didi

java - 如何使用面板管理我的 java applet?

转载 作者:行者123 更新时间:2023-12-02 08:18:23 27 4
gpt4 key购买 nike

我使用 java 编写了一个小每周支付小程序。但当谈到获得一个像样的布局时,我却一无所知。我开始将面板和网格布局添加到代码中。但我不知道如何实现它们。

这是代码。

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;

public class WeeklyPayCalculatorApplet extends Applet implements ActionListener {

//define variables, Button, label, TextField
//Create a Button class




Label title1 = new Label("Western Distribution");
Label title2 = new Label("Weekly Payroll Calculation");
Label lblEnterMins = new Label("Enter Total Minutes Worked: ");
Label lblEnterHours = new Label("Enter Hourly Pay Rate: ");
Label lblHoursWorked = new Label("Hours Worked:");
Label lblLeftOver = new Label("Leftover Miutes:");
Label lblWeekPay = new Label("Weekly Pay:");

Label hoursWorked = new Label(" ");
Label leftoverMins = new Label(" ");
Label weeklyPay = new Label(" ");

TextField txtMinutes = new TextField(10);
TextField txtRate = new TextField(10);


Button btnPay = new Button("Weekly Pay");
Button btnClear = new Button("Clear");
Button btnExit=new Button("Exit");


//Panels

Panel pnlCenter = new Panel();
Panel pnlSouth = new Panel();
Panel pnlNorth = new Panel();

//Grids
GridLayout gridLay1 = new GridLayout(1,3);


public void init() {
// add the displayable objects;
add("Center", pnlCenter);
pnlCenter.setLayout(gridLay1);
pnlCenter.add(title1);
pnlCenter.add(title2);
add(lblEnterMins);
add(txtMinutes);
add(lblEnterHours);
add(txtRate);

add(btnPay);
add(btnClear);
add(btnExit);

add(lblHoursWorked);
add(hoursWorked);
add(lblLeftOver);
add(leftoverMins);
add(lblWeekPay);
add(weeklyPay);




//Attach event to Button
btnPay.addActionListener(this);
btnClear.addActionListener(this);
btnExit.addActionListener(this);

}

public void paint(Graphics g) {
//Draw any pictures
//Make sure the picture is in the same directory as the .class files


}

public void actionPerformed(ActionEvent e) {
//This method will fire when button is pressed
//define temporary variables

Integer hour = 60;
Double dblWeeklyPay;
Integer intLeftOvers;
Integer inthoursWorked;



Integer intMinutes = Integer.parseInt(txtMinutes.getText());
Double dblRate = Double.parseDouble(txtRate.getText());

inthoursWorked = intMinutes / hour;

intLeftOvers = 600 - intMinutes;


dblWeeklyPay = (intMinutes / hour) * dblRate;



hoursWorked.setText(inthoursWorked);
leftoverMins.setText(intLeftOvers);
weeklyPay.setText(dblWeeklyPay);




}

}

最佳答案

说到布局,请阅读 Using Layout Managers将是一个好的开始。我个人喜欢使用GridBagLayout因为它给了你很大的控制权,但根据你的需要,它可能会太过分。

但如果您觉得它太令人畏惧,您可以随时使用 NetbeansEclipse (eclipse 更开源,也是我最喜欢的)以可视化方式设计你的 GUI。

关于java - 如何使用面板管理我的 java applet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5918188/

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