你在代码中并没有指明oo1使用的Layout,在这种情况下,所有的容器默认的layout都是FlowLayout,所以你的界面布局就会出现这种情况。想实现你要的效果,需要使用一个BorderLayout和一个FlowLayout。
这两句话不要注释,继续留着:
oo1.setBorder((Border) new BorderLayout())
oo1.setLayout(new BorderLayout())
这两句话直接删掉,没什么用。
oo2.setLayout(null)
oo3.setLayout(null)
你想要的效果,使用单纯的BorderLayout达不到效果,需要使用多个Layout嵌套使用才行。
下面是一个用Java Design生成的界面,是你要的效果吧?
相关代码如下:
Application1.java
import java.awt.Toolkit
import javax.swing.SwingUtilities
import java.awt.Dimension
public class Application1 {
boolean packFrame = false
public Application1() {
Frame1 frame = new Frame1()
if (packFrame) {
frame.pack()
} else {
frame.validate()
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()
Dimension frameSize = frame.getSize()
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2)
frame.setVisible(true)
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Application1()
}
})
}
}
Frame1.java
import java.awt.BorderLayout
import java.awt.Dimension
import javax.swing.JFrame
import javax.swing.JPanel
import java.awt.FlowLayout
import javax.swing.JButton
public class Frame1 extends JFrame {
public Frame1() {
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1)
jButton1.setText("jButton1")
jPanel1.setLayout(flowLayout1)
flowLayout1.setAlignment(FlowLayout.RIGHT)
jButton2.setText("jButton2")
jPanel1.add(jButton2)
jPanel1.add(jButton1)
this.getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH)
}
BorderLayout borderLayout1 = new BorderLayout()
JPanel jPanel1 = new JPanel()
FlowLayout flowLayout1 = new FlowLayout()
JButton jButton1 = new JButton()
JButton jButton2 = new JButton()
}
好好理解下sender的含义。它是发起这次事件的发起者,也就是你的buttonprivate void mine_click(object sender, System.EventArgs e)
{
credit = credit+ 1
label1.Text = "credit:" + credit.ToString()
///////我想在这里放入代码让被点击的按钮删除掉/////////
Button bt=sender as Button
flowLayoutPanel1.Controls.Remove(bt)
}
/* ZL编编程实现:有一个标题为“计算”的窗口,窗口的布局为FlowLayout有4个按钮,分别为加、减、乘、除;还有3个文本行
* 单击任一按钮,将两个文本行的数字进行相应的运算,在第三个文本行中显示结果。
*/
package SetEvent
import java.awt.*
import java.awt.event.*
public class SetCalculator extends Frame implements ActionListener {
static SetCalculator frm = new SetCalculator()
static Button btn1,btn2,btn3,btn4
static TextField txt1,txt2,txt3
public static void main(String[] args) {
frm.setTitle("计算器")
frm.setBounds(900,600,240,200)
frm.setLayout(new FlowLayout())
frm.setVisible(true)
btn1 = new Button("加")
btn2 = new Button("减")
btn3 = new Button("乘")
btn4 = new Button("除")
btn1.addActionListener(frm)
btn2.addActionListener(frm)
btn3.addActionListener(frm)
btn4.addActionListener(frm)
txt1 = new TextField(10)
txt2 = new TextField(10)
txt3 = new TextField(10)
txt3.setEditable(false)
frm.add(txt1)
frm.add(txt2)
frm.add(btn1)
frm.add(btn2)
frm.add(btn3)
frm.add(btn4)
frm.add(txt3)
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1) {
int sum
sum=Integer.parseInt(txt1.getText())+Integer.parseInt(txt2.getText())
String Sum=String.valueOf(sum)
txt3.setText(Sum)
}
if(e.getSource()==btn2) {
int sum
sum=Integer.parseInt(txt1.getText())-Integer.parseInt(txt2.getText())
String Sum=String.valueOf(sum)
txt3.setText(Sum)
}
if(e.getSource()==btn3) {
int sum
sum=Integer.parseInt(txt1.getText())*Integer.parseInt(txt2.getText())
String Sum=String.valueOf(sum)
txt3.setText(Sum)
}
if(e.getSource()==btn4) {
int sum
sum=Integer.parseInt(txt1.getText())/Integer.parseInt(txt2.getText())
String Sum=String.valueOf(sum)
txt3.setText(Sum)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)