- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Java 初学者,我在使用 static final
变量时遇到了一些问题。我有一个登录页面,可以从中获取当前登录用户的用户名。
登录页面代码
import javax.swing.*;
public class ProfileLogin extends JFrame {
public ProfileLogin() {
initComponents();
}
private void initComponents() {
jPanel1 = new JPanel();
proName = new JTextField();
jButton2 = new JButton();
jLabel2 = new JLabel();
jPanel2 = new JPanel();
logName = new JTextField();
jButton1 = new JButton();
jLabel1 = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Sound Recognition System");
setResizable(false);
jPanel1.setBorder(BorderFactory.createTitledBorder("Create Profile"));
jButton2.setText("Create New");
jButton2.setToolTipText("Click to create a new profile");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel2.setText("Choose a Profile Name:");
GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(proName, GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
.addComponent(jLabel2)
.addComponent(jButton2, GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(proName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addGap(16, 16, 16))
);
jPanel2.setBorder(BorderFactory.createTitledBorder("Select Profile"));
jButton1.setText("Login");
jButton1.setToolTipText("Click to Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel1.setText("Enter your Profile Name:");
GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(logName, GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
.addComponent(jButton1, GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(logName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addGap(23, 23, 23))
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel1, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-195)/2, (screenSize.height-294)/2, 195, 294);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String profileName;
String empty;
empty = "";
profileName = proName.getText();
try {
if(profileName.equalsIgnoreCase("")) {
JOptionPane.showMessageDialog(null, "Please Enter a Profile Name!");
return;
}
database.rs = database.st.executeQuery("select pname from login where pname='"+profileName+"'");
if(database.rs.next()) {
JOptionPane.showMessageDialog(null, "Username Already Existing!");
return;
}
database.st.executeUpdate("insert into login(pname) values('"+profileName+"')");
JOptionPane.showMessageDialog(null, "Profile successfully created!");
proName.setText(empty);;
}
catch(Exception e)
{}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String loginName=logName.getText().trim();
if(loginName.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(null, "Please Enter Profile Name!");
return;
}
database.rs = database.st.executeQuery("select pname from login where pname='"+loginName+"'");
if (database.rs.next()) {
Account a = new Account();
a.setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null, "No such username!");
}
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProfileLogin().setVisible(true);
}
});
}
private JButton jButton1;
private JButton jButton2;
private JLabel jLabel1;
private JLabel jLabel2;
private JPanel jPanel1;
private JPanel jPanel2;
private JTextField logName;
private JTextField proName;
private DatabaseConnection database = new DatabaseConnection();
}
现在我有一个java页面,我需要将登录页面中的用户名嵌入到静态最终变量中。但我不知道如何从登录页面传递变量并将值嵌入到静态最终变量中。
import javax.sound.sampled.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SoundRecording extends JDialog {
String name="JACOB";
public static final String SAVE_PATH = "./data/recordings/"+name;
private JLabel operationLabel;
private JPanel recordPanel;
private JButton applyButton;
private JButton cancelButton;
private JComboBox operationList;
private JButton playButton;
private JButton recordButton;
private JButton stopButton;
private JSeparator separator;
private AudioVisualizer visualizer;
private AudioInputStream audioStream;
private SourceDataLine sourceLine;
private AudioFormat audioFormat;
private TargetDataLine line;
private ByteArrayOutputStream output;
public SoundRecording(JFrame parent) {
super(parent, "Record", true);
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
recordPanel = new JPanel();
recordButton = new JButton();
stopButton = new JButton();
playButton = new JButton();
operationLabel = new JLabel("Select Operation:");
operationList = new JComboBox();
applyButton = new JButton();
cancelButton = new JButton();
separator = new JSeparator();
visualizer = new AudioVisualizer();
recordPanel.setBorder(BorderFactory.createTitledBorder("Options"));
recordButton.setText("Record");
recordButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
recordButtonActionPerformed(evt);
}
});
stopButton.setText("Stop");
stopButton.setEnabled(false);
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
stopButtonActionPerformed(evt);
}
});
playButton.setText("Play");
playButton.setEnabled(false);
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
playButtonActionPerformed(evt);
}
});
operationList.setModel(new DefaultComboBoxModel(new String[] { "Shutdown", "Sleep", "Close", "Minimize" }));
applyButton.setText("Apply");
applyButton.setEnabled(false);
applyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
applyButtonActionPerformed(evt);
}
});
cancelButton.setText("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
GroupLayout recordPanelLayout = new GroupLayout(recordPanel);
recordPanel.setLayout(recordPanelLayout);
recordPanelLayout.setHorizontalGroup(
recordPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(recordPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(operationList, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
.addComponent(separator, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
.addComponent(visualizer, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
.addGroup(recordPanelLayout.createSequentialGroup()
.addGroup(recordPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(operationLabel, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
.addGroup(recordPanelLayout.createSequentialGroup()
.addComponent(recordButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(stopButton, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE))
.addComponent(playButton, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)))))
);
recordPanelLayout.setVerticalGroup(
recordPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(recordPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(operationLabel)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(operationList, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(separator, GroupLayout.PREFERRED_SIZE, 10, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(visualizer, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(recordPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(recordButton)
.addComponent(stopButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(playButton)
.addContainerGap(10, Short.MAX_VALUE))
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addComponent(applyButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelButton)
.addGap(0, 8, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(recordPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(recordPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(applyButton)
.addComponent(cancelButton))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
setSize(215, 299);
setResizable(false);
/*
* This is used to position the frame into
* the center of the screen.
*/
setLocationRelativeTo(null);
}
private void recordButtonActionPerformed(ActionEvent evt) {
recordButton.setEnabled(false);
stopButton.setEnabled(true);
playButton.setEnabled(false);
applyButton.setEnabled(true);
/*
* Construct the data line from the microphone using
* the custom audio format.
*/
try {
audioFormat = Microphone.getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
line = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
} catch(Exception ex) {
ex.printStackTrace();
}
/*
* Create a thread to capture the microphone data
* into an audio file and start the thread running.
*/
CaptureThread capture = new CaptureThread();
capture.start();
}
private void stopButtonActionPerformed(ActionEvent evt) {
stopButton.setEnabled(false);
recordButton.setEnabled(true);
playButton.setEnabled(true);
line.stop();
line.close();
}
private void playButtonActionPerformed(ActionEvent evt) {
playButton.setEnabled(false);
PlaybackThread playback = new PlaybackThread();
playback.start();
}
private void applyButtonActionPerformed(ActionEvent evt) {
/*
* Set the file type and the file extension.
*/
String operation = operationList.getSelectedItem().toString();
File audioFile = new File(SAVE_PATH + operation + ".wav");
/*
* Display the confirmation dialog.
*/
if(audioFile.exists()) {
Toolkit.getDefaultToolkit().beep();
Object[] options = {"Yes", "No",};
int returnValue = JOptionPane.showOptionDialog(this,
"This recording already exists, do you want to overwrite?", "Overwrite",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
if(returnValue == JOptionPane.NO_OPTION) {
return;
}
}
/*
* Write the stored buffer bytes into the
* saved audio file.
*/
byte[] audioData = output.toByteArray();
ByteArrayInputStream stream = new ByteArrayInputStream(audioData);
try {
AudioInputStream audioStream = new AudioInputStream(stream, audioFormat, audioData.length);
AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, audioFile);
} catch(IOException ex) {
ex.printStackTrace();
}
this.dispose();
}
private void cancelButtonActionPerformed(ActionEvent evt) {
this.dispose();
}
/*
* Inner class to capture data from microphone and
* write it to an output audio file.
*/
class CaptureThread extends Thread implements Runnable {
@Override
public void run() {
/*
* Start writing audio data into the data line.
*/
try {
/*
* Close the current running line while we
* listen for input on a new line.
*/
Microphone.getLine().stop();
Microphone.getLine().close();
/*
* Open the new line for recording
* sound clips.
*/
line.open(audioFormat);
line.start();
/*
* Start the drawing of the visualizer.
*/
visualizer.start();
/*
* Create a buffer which the data can be stored in
* and listen for input data.
*/
output = new ByteArrayOutputStream();
while(true) {
if(!line.isOpen()) {
break;
}
/*
* Get the total about of bytes which
* are available on the line.
*/
int available = line.available();
/*
* Make sure something is pending on
* the line before trying to read.
*/
if(available > 0) {
byte[] buffer = new byte[available];
int read = line.read(buffer, 0, buffer.length);
if(read > 0) {
output.write(buffer, 0, buffer.length);
}
/*
* Set the chunk of data into the
* visualizer.
*/
visualizer.setData(buffer);
}
}
/*
* Close the ouput buffer.
*/
output.close();
/*
* Stop the drawing of the visualizer.
*/
visualizer.stop();
/*
* Re-open the line and start it running
* again.
*/
Microphone.getLine().open(audioFormat);
Microphone.getLine().start();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
/*
* This class is used for playing back audio which is
* saved into a buffer. This feeds the audio into the
* output speaker line.
*/
class PlaybackThread extends Thread implements Runnable {
@Override
public void run() {
byte[] audioData = output.toByteArray();
ByteArrayInputStream stream = new ByteArrayInputStream(audioData);
/*
* Get the data line to the output speakers.
*/
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
try {
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch(Exception ex) {
ex.printStackTrace();
}
/*
* Start the audio output line.
*/
sourceLine.start();
/*
* Write the data into the audio stream.
*/
int read = 0;
byte[] data = new byte[stream.available()];
while(read != -1) {
try {
read = stream.read(data, 0, data.length);
} catch(Exception e) {
e.printStackTrace();
}
if(read > 0) {
@SuppressWarnings("unused")
int written = sourceLine.write(data, 0, read);
}
}
/*
* Flush any unused bytes from the stream and
* close the audio line to the speakers.
*/
sourceLine.drain();
sourceLine.close();
/*
* Re-enable the play button, allowing the user to
* play the clip again.
*/
playButton.setEnabled(true);
}
}
}
用户名必须添加到静态最终变量SAVE_PATH
中,这样它就会变成“./data/recordings/用户名”
我尝试直接添加示例用户名,如下所示
String name="JACOB";
public static final String SAVE_PATH = "./data/recordings/"+name;
但它说不能从静态上下文中引用非静态变量名称。
有人可以帮我解决这个问题吗?
最佳答案
最简单的方法是将 name
也设为静态:
static String name = "JACOB";
但通常,您希望在成员方法中创建非静态的路径:
final String path = SAVE_PATH + name;
由于我看不到您如何使用 name
变量,因此我无法真正告诉您在您的情况下正确的方法是什么。
关于java - 如何处理静态最终变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10068508/
考虑需要与 iOS 5 和 iOS 6 兼容的应用。 有没有办法标记纯粹为了 iOS 5 兼容性而存在的代码,以便当部署目标最终更改为 iOS 6 时它显示为编译错误(或警告)? 像这样: #IF_D
我想我知道答案但是...有什么方法可以防止全局变量被稍后执行的 修改吗? ?我知道全局变量首先是不好的,但在必要时,有没有办法让它成为“最终”或“不可变”?欢迎黑客/创造性的解决方案。谢谢 最佳答案
class Foo { final val pi = 3 } 是否每Foo对象有一个 pi成员?因此我应该把 pi在伴生对象中? 最佳答案 如果您担心内存占用,您可以考虑将此字段移动到伴随对象中。
随着可用的 Web 开发框架种类繁多,似乎总是有一种“尝试新事物”的永久动机。因此,我们中的一些人发现自己用一个框架换另一个框架,从来没有对最终结果完全满意。当然,总会有一个特定的 Web 框架可以完
在MDN中指出, If the finally block returns a value, this value becomes the return value of the entire try
我正在尝试用 JavaScript 制作一个基本的井字棋类型游戏。尽管 x 和 y 值在 if 语句的范围内,但除最后一个之外的所有空格都有效。 我不知道为什么最后的 else if 语句不起作用。
我想知道如何使用PowerMock模拟kotlin最终类(class),以便进行测试。我按照指南测试了Java最终类,但仍然出现此错误 Cannot subclass final class 有什么办
考虑以下设置: // debugger class public class Debug { // setting public final static boolean DEBUG
给定以下类(class): public class SomeClass { private final int a; public SomeClass(int a) {
This question already has answers here: What does “final” do if you place it before a variable?
我有一个类PasswordEncryptor,它使用org.jasypt.util.password.StrongPasswordEncryptor作为其字段之一,因为我试图使应用程序“可集群”所有类
我今天有一个关于 StreamReader 类的问题。具体使用文件名参数初始化此类例如: TextReader tr = new StreamReader(fileName); 显然,当此操作完成后,
我想弄清楚什么是使用带锁的 try/finally 的最佳方式。 当我在同一个地方有 lock() 和 unlock() 时,我只使用 try/finally block 作为 JavaDoc还建议:
在 Java 中序列化后是否可以将 final transient 字段设置为任何非默认值?我的用例是一个缓存变量——这就是它是 transient 的原因。我还有一个习惯,就是制作不会改变的 Map
在this问题说 final transient 字段在序列化后不能设置为任何非默认值。那么,为什么我为 aVar1 变量设置了 3,为 aVar3 变量设置了 s3? import java.io.
在Xbox上进行开发时,我使用的是F#规范中最终工作流程的修改版。 Xbox上的.net框架似乎不支持尾部调用。因此,我必须在编译时禁用尾部调用优化。 尽管起初看来这种限制会阻止在计算表达式中使用任何
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想让我的带有自定义对象的ArrayList成为最终对象,以便对象在设置后无法更改。 我试图这样声明它: private final ArrayList XML = new ArrayList();
我有一个场景,我需要类似于 .NET 的 try-catch-finally block 的内容。 在我的尝试中,我将创建一个#temp表,向其中插入数据并基于#temp处理其他数据集。 先是CATC
对此可能有一个简单的答案,但尝试充分使用 Butterknife,将一些 findViewById 转换为 @BindViews,并注意到我无法在需要声明为 Final 的 View 上使用 Bind
我是一名优秀的程序员,十分优秀!