我有一个JMenu,我想根据菜单中的按钮更改窗口的内容.我设法将面板显示为弹出窗口,但我希望它与菜单显示在同一窗口中.到目前为止这是我的代码:
public class GUImenu extends JFrame
{
private JMenuBar menuBar;
private JMenu menu;
private JMenu subMenu;
private JMenuItem item1;
private JMenuItem item2;
private JMenuItem item3;
private JMenuItem item4;
private JMenuItem item5;
private JMenuItem item6;
public GUImenu()
{
super("Example Menu System");// Call the JFrame constructor.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Specify an action for the close button.
buildMenuBar();
// Pack and display the window.
pack();
setSize(1000, 250); // set frame size
setVisible(true);
}
private void buildMenuBar()
{
// Create the menu bar.
menuBar = …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个简单的Java游戏 - 通过轨道驾驶汽车.我的老师习惯使用矩形和类似的东西.我的问题是当我使用repaint()我的框架闪烁的方法时.我试图在汽车改变方向时放置重绘方法,但它没有任何好处.有什么建议?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MAP extends JFrame
{
//the URL and Img designed for the images
URL cLeft,cRight,cUp;
Image img1,img2,img3;
//these will keep track of each player’s speed:
double p1Speed =.5, p2Speed =.5;
//constant for the screen size and used for the drawing the terrain
final int WIDTH = 900, HEIGHT = 650;
//these are ints that represent …Run Code Online (Sandbox Code Playgroud) 我有一个固定大小(300 x 33)的JTextArea,启用了换行,我已添加到JPanel内部.每当在其中键入并经过最后一个可见行(在本例中为第二个)时,文本将继续在文本区域的视图之外,并被隐藏.
是否有限制在JTextArea内部输入的文本限制为文本区域的大小而不是它包含的字符数(使其依赖于不同字体的每个字符占用的不同空间)?
编辑:我忘了提到不修复JTextArea的大小并在构造函数中提供行号和列号会导致文本区域拉伸并填充持有它的JPanel.我相信这是因为面板的布局是BoxLayout.
我使用一些html标签制作了用于摆动的工具提示
_graph.setToolTipText("<html><div style=\"width: 300px; height: 100px;"
+ " overflow: auto; border: 0;<p style=\"padding:2 5 2 5;\"></div>Please Wait...");
Run Code Online (Sandbox Code Playgroud)
这 _graph是组件的对象.问题是,如果数据超出我需要滚动但它没有发生.请任何人建议我做滚动条.
JTable包括时间字段,例如"01:50".我需要将此值读入整数变量.为此,我想将时间转换为分钟.例如,"01:50"应转换为110.
要解决此任务,首先我将时间值保存为String.
String minutS = tableModel.getValueAt(1,1).toString();
Run Code Online (Sandbox Code Playgroud)
其次,我将处理此String并提取符号前后的整数:.最后,我将计算总分钟数.这个解决方案有效吗?也许,有可能用日历代替它或者像这样吗?
我正在创建一个使用GUI来显示抵押付款的Java程序.我正在尝试向textArea输出将用于抵押的付款.这是我的代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;
public class MortgageGui extends JFrame implements ActionListener {
// Set two-places for decimal format
DecimalFormat twoPlaces = new DecimalFormat("$0.00");
// Declare variable for calculation
Double loanAmt;
double interestRate;
double monthlyPayment;
int payment;
String amount;
JTextField loanAmount;
JComboBox loanTypeBox;
JLabel paymentOutput;
JTextArea paymentList;
// Build arrays for mortgages
double[] mortgage1 = {7.0, 5.35, 0.0}; // (years, interest, monthly payment)
double[] mortgage2 = {15.0, 5.5, 0.0}; // (years, interest, monthly payment)
double[] mortgage3 …Run Code Online (Sandbox Code Playgroud) 我有一个JTextArea并且我正在检测是否有任何文本是选择,如果没有,那么两个菜单项是灰色的.我遇到的问题是,当我编译并打开应用程序时,我必须首先单击JTextArea,然后菜单项显示为灰色,如果我不这样做,即使没有选择文本也不是.我正在使用以下插入符号监听器.
textArea.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent arg0) {
int dot = arg0.getDot();
int mark = arg0.getMark();
if (dot == mark) {
copy2.setEnabled(false);
cut1.setEnabled(false);
}
else{
cut1.setEnabled(true);
copy2.setEnabled(true);
}
}
});
Run Code Online (Sandbox Code Playgroud) 尝试将一个mouseAdapter添加到JButton以右键单击以标记该单元格.问题是当我将它实例化到按钮上时,它不会让我.也许是因为它上面已经有一个actionlistener?我不太确定.任何帮助表示赞赏.我正在创造一个扫雷游戏,fyi.
button = new JButton[size][size];
ButtonListener bl = new ButtonListener();
for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
button[r][c] = new JButton("");
button[r][c].addActionListener(bl);
button[r][c].addMouseListener (new MouseAdapter());``
// error message: cannot instantiate the type MouseAdapter
panel.add(button[r][c]);
Run Code Online (Sandbox Code Playgroud)
最终如果有效,我想将其融入游戏中:
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) { // if right click
button.setText("F");
button.getModel().setPressed(false);
// button.setEnabled(true);
} else {
button.setText("X");
button.getModel().setPressed(true);
// button.setEnabled(false);
}
}
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个模拟记录存储的简单GUI.我还处于起步阶段.
当我尝试添加文本来描述用户期望在文本字段中输入的内容时,我遇到了麻烦.
另外,我也无法将每个文本字段放在自己的行上.换句话说,如果一行中有两个文本字段的空间,那么它将显示在一行中,并且我试图在其自己的行上显示每个文本字段.
这是我到目前为止所尝试的:
item2 = new JTextField("sample text");
Run Code Online (Sandbox Code Playgroud)
但是上面的代码只是在文本字段中添加了默认文本,这不是我需要的:/
我提前感谢所有的帮助.
public class MyClass extends JFrame{
private JTextField item1;
private JTextField item2;
public MyClass(){
super("Matt's World of Music");
setLayout(new FlowLayout());
item1 = new JTextField();
item2 = new JTextField();
add(item1);
add(item2);
thehandler handler = new thehandler();
item1.addActionListener(handler);
item2.addActionListener(handler);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现以下要求:
JTextield 中只接受数字,当按下非数字键时,将不被接受。
我尝试了很多东西,甚至尝试调用退格事件来删除最后一个字符(如果它是非数字)。但是,无法删除在文本字段中键入的值。我试图理解 DOCUMENT FILTER 但发现它很难实现。如果有人帮助我解决问题,我会很高兴。