Tôi làm cách nào để mở một cửa sổ ứng dụng mới từ chính applet?Cách mở cửa sổ ứng dụng mới từ một applet
6
A
Trả lời
6
Để mở một cửa sổ Java mới (JFrame
) từ một applet, xem trích sau đây từ Java tutorial:
//1. Create the frame.
JFrame frame = new JFrame("FrameDemo");
//2. Optional: What happens when the frame closes?
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//3. Create components and put them in the frame.
//...create emptyLabel...
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
//4. Size the frame.
frame.pack();
//5. Show it.
frame.setVisible(true);
Để mở một cửa sổ trình duyệt mới mà cũng chứa một applet showDocument(URL, "_blank")
:
URL url = new URL(getCodeBase().getProtocol(),
getCodeBase().getHost(),
getCodeBase().getPort(),
"/next.html");
getAppletContext().showDocument(url, "_blank");
0
package com.ashok.test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class ChessBoardWithColumnsAndRows {
private final JPanel gui = new JPanel(new BorderLayout(3, 3));
private JButton[][] chessBoardSquares = new JButton[8][8];
private JPanel chessBoard;
private final JLabel message = new JLabel(
"Chess Champ is ready to play!");
private static final String COLS = "ABCDEFGH";
ChessBoardWithColumnsAndRows() {
initializeGui();
}
public final void initializeGui() {
// set up the main GUI
gui.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);
tools.add(new JButton("New")); // TODO - add functionality!
tools.add(new JButton("Save")); // TODO - add functionality!
tools.add(new JButton("Restore")); // TODO - add functionality!
tools.addSeparator();
tools.add(new JButton("Resign")); // TODO - add functionality!
tools.addSeparator();
tools.add(message);
gui.add(new JLabel("?"), BorderLayout.LINE_START);
chessBoard = new JPanel(new GridLayout(0, 9));
chessBoard.setBorder(new LineBorder(Color.BLACK));
gui.add(chessBoard);
// create the chess board squares
Insets buttonMargin = new Insets(0,0,0,0);
for (int ii = 0; ii < chessBoardSquares.length; ii++) {
for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
ImageIcon icon = new ImageIcon(
new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
//) {
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
chessBoardSquares[jj][ii] = b;
}
}
//fill the chess board
chessBoard.add(new JLabel(""));
// fill the top row
for (int ii = 0; ii < 8; ii++) {
chessBoard.add(
new JLabel(COLS.substring(ii, ii + 1),
SwingConstants.CENTER));
}
// fill the black non-pawn piece row
for (int ii = 0; ii < 8; ii++) {
for (int jj = 0; jj < 8; jj++) {
switch (jj) {
case 0:
chessBoard.add(new JLabel("" + (ii + 1),
SwingConstants.CENTER));
default:
chessBoard.add(chessBoardSquares[jj][ii]);
}
}
}
}
public final JComponent getChessBoard() {
return chessBoard;
}
public final JComponent getGui() {
return gui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
ChessBoardWithColumnsAndRows cb =
new ChessBoardWithColumnsAndRows();
JFrame f = new JFrame("ChessChamp");
f.add(cb.getGui());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// ensures the minimum size is enforced.
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Các vấn đề liên quan
- 1. JavaFX mở cửa sổ mới
- 2. Cách mở tệp mới trong vim trong cửa sổ mới
- 3. Mở tệp mới trong cửa sổ gui mới hoặc bằng cách tách cửa sổ
- 4. mở cửa sổ mới bằng a4j: commandButton
- 5. Làm mới cửa sổ cha mẹ từ cửa sổ con bằng cách sử dụng javascript
- 6. Mở URL SSRS trong Cửa sổ mới
- 7. Mở hình ảnh trong cửa sổ mới
- 8. Mở phản hồi trên cửa sổ mới
- 9. Google CSE mở trong cửa sổ mới
- 10. Nút mở trong cửa sổ mới?
- 11. Eclipse: Mở trong Cửa sổ mới
- 12. mở một cửa sổ mới và gọi hàm javascript
- 13. ASP.NET Response.Redirect để mở một cửa sổ trình duyệt mới?
- 14. System.Windows.Forms.WebBrowser mở liên kết trong cùng cửa sổ hoặc cửa sổ mới với cùng một phiên
- 15. Cách di chuyển cửa sổ trình xem applet?
- 16. Ruby/Rails - Mở một URL từ Bộ điều khiển Trong Cửa sổ mới
- 17. Làm cách nào để "Mở trong Cửa sổ mới" bằng cách sử dụng điều khiển WebBrowser?
- 18. Selenium WebDriver: Mở tab mới thay vì cửa sổ mới
- 19. Mở cửa sổ dòng lệnh đến một thư mục được chỉ định từ ứng dụng Cocoa
- 20. không thể mở dịch vụ cửa sổ trên máy tính '.' trong ứng dụng cửa sổ
- 21. ASP.NET MVC mở tập tin pdf trong cửa sổ mới
- 22. Cách mở trang trong cửa sổ hoặc tab mới từ mã sau
- 23. Làm cách nào để mở cửa sổ thứ hai từ cửa sổ đầu tiên trong wpf?
- 24. Mở trang trong cửa sổ mới không có cửa sổ bật lên
- 25. XNA với Windows Forms - Mở cửa sổ mới
- 26. Làm cách nào để mở một cửa sổ trên một chuỗi mới?
- 27. Mở tệp liên quan đến rails.vim trong cửa sổ mới
- 28. Java Mở một cửa sổ mới bằng cách nhấp vào một nút
- 29. Tạo cửa sổ mới bằng cách sử dụng jquery
- 30. Mở cửa sổ phía sau cửa sổ hiện tại bằng cách sử dụng Javascript/jQuery
Câu trả lời không có văn bản nds được downvoted bởi cộng đồng. Vui lòng thêm một số giải thích cho mã của bạn. – Safiyya