我想并排添加两个jPanel到JFrame.两个框是jpanels,外框是jframe
我有这些代码行.我有一个名为seatinPanel的类,它扩展了JPanel,在这个类中我有一个构造函数和一个名为utilityButtons的方法,它返回一个JPanel对象.我希望utilityButtons JPanel在右侧.我这里的代码只在运行时显示utillityButtons JPanel.
public guiCreator()
{
setTitle("Passenger Seats");
//setSize(500, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
seatingPanel seatingPanel1 = new seatingPanel();//need to declare it here separately so we can add the utilityButtons
contentPane.add(seatingPanel1); //adding the seats
contentPane.add(seatingPanel1.utilityButtons());//adding the utility buttons
pack();//Causes this Window to be sized to fit the preferred size and layouts of its subcomponents
setVisible(true);
}
Run Code Online (Sandbox Code Playgroud) 我试图改变源的背景颜色,但java给我内部致命错误发生消息.你能告诉我我做错了吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonPanel extends JPanel
{
private JButton yellowButton = new JButton("Yellow");
public ButtonPanel()//constructor
{
//setLayout is a method in JPanel that is called in this constructor
setLayout (new FlowLayout(FlowLayout.LEFT));
add(yellowButton);
actions actionsSpecified = new actions();
yellowButton.addActionListener(actionsSpecified);
}
}
class actions implements ActionListener
{
//actionPerformed is an interface that muse be overriden
public void actionPerformed(ActionEvent evt)
{
// knows where the event is generated from(from a button, scrollbar etc...)
JButton source = (JButton) evt.getSource(); …
Run Code Online (Sandbox Code Playgroud) 我有这些代码行.我知道你不能将非final变量传递给内部类,但我需要将变量传递i
给匿名内部类以用作seatID.你能建议这样做的方法吗?
JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
for (int i = 0; i < 40; i++)
{
seats[i] = new JButton();//creating the buttons
seats[i].setPreferredSize(new Dimension(50,25));//button width
panel4seating.add(seats[i]);//adding the buttons to the panels
seats[i].addActionListener(new ActionListener()
{ //anonymous inner class
public void actionPerformed(ActionEvent evt)
{
String firstName = (String)JOptionPane.showInputDialog("Enter First Name");
String lastName = (String)JOptionPane.showInputDialog("Enter Last Name");
sw101.AddPassenger(firstName, lastName, seatingID);
}
});
}
Run Code Online (Sandbox Code Playgroud) 在大多数代码示例中,我看到人们这样做.
import javax.swing.*; // for the frame
import java.awt.*; // for the checkBox and the label
import java.awt.event.*; // for the checkBox listener
Run Code Online (Sandbox Code Playgroud)
如果我说的是导入java.awt.*它是正确的,它会导入其中的所有内容,因此不需要说import java.awt.event.*; 还是有提速?任何人都可以解释导入库的作用,是导入一个简单的文本类,包含在源代码中还是告诉jvm包含导入的字节代码?所以在java中导入只会切换命名空间,所以我不必输入长类名?
我为什么会收到错误.在eclipse中,它表示构造函数调用应该是第一行.这是第一线.或者你不能扩展Main?
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//JLabel testLabel1 = new JLabel();
public Main(){
super("title bar");
}
}
}
Run Code Online (Sandbox Code Playgroud) (pass[i]!= null) && (pass[i].getName()!= "nullnull")
< - 当我调试它时返回true,即使我在调试pass[i].getName() == "nullnull"
时使用eclipse中的Expressions窗口检查它时的值
我使用输入对话框输入两个名字
String firstName = (String)JOptionPane.showInputDialog("Enter First Name");
String lastName = (String)JOptionPane.showInputDialog("Enter Last Name");
Run Code Online (Sandbox Code Playgroud)
并返回
public String getName()
{
return FirstName + LastName;
}
Run Code Online (Sandbox Code Playgroud)