如何为JApplet进行布局 [英] How to make a layout for JApplet

查看:189
本文介绍了如何为JApplet进行布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的Sudoku游戏.因为这是我的第一个大",所以我想自己做所有事情(没有我通常用来制作GUI的NetBeans接口设计器).因此,对于GUI,我创建了一个扩展JApplet的类,并在paint()方法中绘制了一个简单的数独字段.

I am creating a simple Sudoku game. Since this is my first "big" i want to do everything myself (without the NetBeans interface designer that i normally use to make GUIs). So for the GUI i created a class that extends JApplet and i drew a simple sudoku field in the paint() method.

现在,我需要输入81个文本字段,每个文本字段将包含1个数字.如何在屏幕上放置它们?另外,我正在考虑制作一个数组,以便能够使用一个for循环更改字段的前向矩阵.

Now i need to make 81 text fields each of which will take in 1 number. How do i position them on the screen? Also, i was thinking of making an array so i'll be able to change the enitre matrix of fields with one for loop.

推荐答案

建议:

  • 永远不要直接在顶级组件(例如JApplet,JFrame,JDialog等)的paint方法中绘制.
  • 如果需要进行自定义绘制,请在扩展JComponent的组件(例如JPanel或JComponent本身)的paintComponent(...)方法重写中执行此操作.
  • 您的问题实际上并不需要定制绘画,至少现在还不需要,并且可以通过其他方法更好,更简单地解决.
  • 使用JLabels,JTextFields等组件,并使用Swing布局管理器(上面提到的其他方法)放置它们.您可以在此处找到有关如何使用它的教程:在容器
  • 首先要关注的布局包括Sudoku单元"的GridLayout和整个GUI的BorderLayout.至少在开始时,请避免使用GridBagLayout和GroupLayout.
  • 请记住,您可以通过嵌套每个都使用简单布局管理器的JPanel来创建复杂的应用程序.
  • 一种简单的绘制"网格线的方法是设置使用GridLayout并将JTextFields保留为Color.BLACK的JPanel的背景色,并确保给GridBagLayout一个小的垂直和水平间隙,以使黑色显示通过.上面列出的教程将向您展示如何执行此操作.
  • 如果这是我的应用程序,则将我的GUI调整为创建一个保存应用程序的JPanel.然后,如果需要在JApplet中显示它,我将创建一个非常小的应用程序,该应用程序将JApplet子类化,然后在init()方法中,将Sudoku JPanel添加到JApplet的contentPane中.这样,如果我想在一个JFrame中显示我的应用程序,我要做的就是创建另一个小类,该类创建一个JFrame并将Sudoku JPanel添加到JFrame的contentPane中,然后在JFrame,然后是setVisible(true).
  • Never draw directly in the paint method of a top-level component such as a JApplet, JFrame, JDialog, or the like.
  • If you need to do custom drawing, do this instead in the paintComponent(...) method override of a component that extends JComponent such as a JPanel or JComponent itself.
  • Your problem doesn't really require custom painting, at least not yet, and is much better and more simply solved by other means.
  • Use components such as JLabels, JTextFields, etc... and position them using the Swing layout managers (the other means noted above). You can find the tutorial on how to use this here: Laying Out Components in a Container
  • Layouts to focus on first include GridLayout for your Sudoku "cells" and BorderLayout for the overall GUI. Avoid the GridBagLayout and GroupLayout, at least when starting out.
  • Remember that you can create complex applications by nesting JPanels that each use a simple layout manager.
  • A simple way to "paint" gridlines is to set the background color of the the JPanel that uses GridLayout and holds the JTextFields to Color.BLACK, and be sure to give your GridBagLayout a small vertical and horizontal gap so that the black shows through. The tutorials listed above will show you how to do this.
  • If this were my application, I'd gear my GUI towards creating a JPanel that held the application. Then if I needed to display it in a JApplet, I'd create a very small application that subclasses JApplet and then in the init() method, add my Sudoku JPanel into the JApplet's contentPane. This way, if I wanted to instead display my app in a JFrame, all I'd need to do would be to create another small class that creates a JFrame and add's my Sudoku JPanel into the JFrame's contentPane, then call pack() on the JFrame, and then setVisible(true).

关于您如何将JPanel添加到JApplet的问题,本教程将再次向您展示如何执行此操作.如果您尚未链接到本教程的大索引,则需要这样做: The Really大索引.

Regarding your question on how to add a JPanel to a JApplet, again the tutorials will show you how to do this. If you haven't linked to the tutorial's big index, you will want to do so: The Really Big Index.

一个非常简单的示例如下:

A very simple example goes like so:

import java.lang.reflect.InvocationTargetException;
import javax.swing.*;

public class MyApplet extends JApplet {
   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               getContentPane().add(new MyJPanel());             
            }
         });
      } catch (InterruptedException e) {
         e.printStackTrace();
      } catch (InvocationTargetException e) {
         e.printStackTrace();
      }
   }
}

这篇关于如何为JApplet进行布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆