小编trs*_*trs的帖子

JFrame拦截退出,接近保存数据

我创建了一个窗口,并希望使用windowStateChanged方法拦截出口,以便在应用程序关闭之前保存数据.但是,它似乎没有在数据关闭之前保存数据.我怎么能纠正这个?

见下面的代码:

public class InventoryMainFrame extends JFrame implements WindowStateListener{
    //set up the main window - instantiate the application
  private InventoryInterface inventoryInterface;    //panel that contains menu choices and buttons

  public InventoryMainFrame(){   //main window        
      setTitle("Inventory System");
      setSize (500,500);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(new BorderLayout());
      //setLocationRelativeTo(null);    //center window on the screen           
      inventoryInterface = new InventoryInterface();    //set up the panel that contains menu choices and buttons
      add(inventoryInterface.getMainPane());         //add that panel to this window                   
      pack();
      setVisible(true); 

      //display window on the screen           
  }

public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java swing save exit jframe

3
推荐指数
1
解决办法
8815
查看次数

JLabel.setText()中的换行符

当我使用JLabel.setText()时如何插入换行符?我试图使用Html,但似乎可以使它适用于setText,仅用于jLabel的初始声明

最初声明jlabel时的方法是:

label = new JLabel("<html>Hello World!<br>blahblahblah</html>");
Run Code Online (Sandbox Code Playgroud)

我的代码:

textString += "<html> quantityTextField.getText() +
   theInventory.findItem(UPCTextField.getText()).toString() + <br> </html>";

purchaseInfo.setText( textString);
Run Code Online (Sandbox Code Playgroud)

它显示html标签和方法名称,而不是方法返回的字符串

java swing newline jlabel multiline

3
推荐指数
1
解决办法
1万
查看次数

设置JList以填充它添加到的组件的整个大小

下面是我创建JList的代码.当它在独立的JFrame中创建时,JList会填充整个框架.但是,当我将其创建为JPanel并将其添加到JFrame时,它没有填充组件的大小,为什么?

public class ListBranchesInterface extends JPanel {
private Library theLibrary; // reference to back end
private ArrayList<LibraryBranch> branches;
private DefaultListModel dlm;
private JList list;
private JScrollPane scroll;

public ListBranchesInterface(Library theLibrary) {
    this.theLibrary = theLibrary;
    branches = new ArrayList<LibraryBranch>();
    branches.addAll(theLibrary.getLibraryBranches());

    Iterator<LibraryBranch> iter = branches.iterator();
    dlm = new DefaultListModel();
    while (iter.hasNext()) {

        dlm.addElement(iter.next().toString());

    }
    list = new JList(dlm); // create a JList from the default list model

    scroll = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); // add a scroll pane
                                                        // to the JList …
Run Code Online (Sandbox Code Playgroud)

java swing jpanel jframe jlist

2
推荐指数
1
解决办法
2092
查看次数

在主窗口中切换JPanel

我有一个应用程序,允许用户选择一个选项,并根据用户选择从组件中删除JPanel,添加新的JPanel并重新验证组件

看代码:

          if (c != null) {
                contentPane.remove(c);
            }
            c = new AddBookInterface(theLibrary);
            contentPane.add(c);
            contentPane.revalidate();
            break;
Run Code Online (Sandbox Code Playgroud)

c是一个组件

我有几个JPanels,用户可以切换,交换机正常工作.但是,当我在用户选择时添加此JPanel时,之后添加的JPanel无法正确加载.是什么造成的?

   public class RemoveBookInterface extends JPanel {

private Library theLibrary;

public RemoveBookInterface(Library theLibrary) {
    this.theLibrary = theLibrary;
    setSize(400, 400);
    setLayout(new BorderLayout());
    setVisible(true);
    removeBook(theLibrary);
}

public void removeBook(Library theLibrary) {
    // prompt user for book id of book to remove
    Long ID = Long
            .parseLong(JOptionPane
                    .showInputDialog("Enter the library book ID for the book you want to remove"));
    try {
        // get library book info …
Run Code Online (Sandbox Code Playgroud)

java swing components jpanel joptionpane

2
推荐指数
1
解决办法
695
查看次数

鼠标移动时java绘制线条

我想在我的应用程序中添加一个功能,允许用户通过在起始位置单击鼠标并在结束位置释放它来绘制直线.当鼠标移动直到最终释放时,线应该移动; 类似于使用Microsoft Paint应用程序绘制线条的方式.

如何实现这一点,以便线条在移动时重新绘制,而不重新绘制可能已在该矩形区域中绘制的其他内容?

java awt mouseevent draw graphics2d

2
推荐指数
1
解决办法
3万
查看次数

java:生成一个不在矩形内的随机点

我有很多矩形,我试图生成一个不在任何一个内部的随机点.我创建了一个方法来执行此操作,但似乎这导致我的应用程序冻结,因为它必须在生成有效点之前经过大量的点:

public Point getLegalPoint() {
           Random generator = new Random();
           Point point;
           boolean okPoint = true;
           do {
                   point = new Point(generator.nextInt(975), generator.nextInt(650));
                   for (int i = 0; i < buildingViews.size(); i++) {
                           if (buildingViews.get(i).getBuilding().getRectangle()
                                           .contains(point)) {
                                   okPoint = false;
                                   break;
                           }

                   }
           } while (okPoint == false);
           return point;
   }
Run Code Online (Sandbox Code Playgroud)

有什么我做错了,或者有更有效的方法来做它,以便它不会冻结我的应用程序?

java point awt

1
推荐指数
1
解决办法
1603
查看次数